This is javascript to generate a number sequence for the user to complete.
function generateRandomSequence(length) {
const sequence = [];
let pattern = Math.floor(Math.random() * 3);
switch (pattern) {
case 0:
// Arithmetic sequence
const start = Math.floor(Math.random() * 10) + 1;
const difference = Math.floor(Math.random() * 5) + 1;
for (let i = 0; i < length; i++) {
const number = start + i * difference;
sequence.push(number);
}
break;
case 1:
// Geometric sequence
const base = Math.floor(Math.random() * 5) + 1;
let power = 1;
for (let i = 0; i < length; i++) {
const number = base ** power;
sequence.push(number);
power++;
}
break;
case 2:
// Random sequence
for (let i = 0; i < length; i++) {
const number = Math.floor(Math.random() * 100) + 1;
sequence.push(number);
}
break;
}
return sequence;
}
function displaySequence(sequence) {
const sequenceText = document.getElementById('sequence-text');
sequenceText.innerText = `Sequence: ${sequence.slice(0, -1).join(', ')}`;
}
function checkAnswer() {
const userInput = parseInt(document.getElementById('user-answer').value, 10);
const lastNumber = sequence[sequence.length - 1];
if (isNaN(userInput)) {
alert('Invalid input. Please enter a number.');
} else if (userInput === lastNumber) {
alert('Correct! Your answer is correct.');
generateNewSequence();
} else {
alert(`Incorrect! The correct answer is ${lastNumber}.`);
}
}
function generateNewSequence() {
sequence = generateRandomSequence(sequenceLength);
displaySequence(sequence);
document.getElementById('user-answer').value = '';
}
const sequenceLength = 5; // Length of the number sequence
let sequence = generateRandomSequence(sequenceLength);
displaySequence(sequence);