I'm creating a quiz based on the Fizz Buzz sequence where the user needs to guess the last element in the series.
Example: 422, Fizz, 424, Buzz, ?
Possible answers should be:
- Fizz
- Buzz
- Fizz Buzz
- Integer
And this is where I'm stuck, in case the last element is an int (not Fizz, Buzz, Fizz Buzz) everything works great as the possible answers are correct. But in case it's a word I get a duplicate with my current code, which makes it very easy for the user to guess the right answer. What I want to do is that in case the last element is one of the words, I want to display the number for that word instead. But I'm not sure how to go about it.
Right now it turns out like this:
Example: 422, Fizz, 424, Buzz, ?
Answers:
- Fizz
- Buzz
- Fizz Buzz
- Fizz (But should display 426 instead)
function() {
var startNumber = randomNumber(1);
var range = startNumber + 5;
var fizzBuzz = fizzBuzzGenerator(startNumber, range);
var fizzBuzzArray = fizzBuzz.split(",");
var fizzBuzzLastElement = fizzBuzzArray[fizzBuzzArray.length - 1];
contentElement.innerHTML = `
${fizzBuzzArray[0]}, ${fizzBuzzArray[1]}, ${fizzBuzzArray[2]}, ${fizzBuzzArray[3]}, ?
<br>
<input class="checkbox" type="checkbox" name="false" value="Fizz">Fizz<br>
<input class="checkbox" type="checkbox" name="false" value="Buzz">Buzz<br>
<input class="checkbox" type="checkbox" name="false" value="Fizz Buzz">Fizz Buzz<br>
<input class="checkbox" type="checkbox" name="correct" value="${fizzBuzzLastElement}">${fizzBuzzLastElement}<br>
`;
},
// Generates Fizz Buzz sequence
function fizzBuzzGenerator(start, stop) {
var fizzBuzzString = "";
for (let i = start; i < stop; i++) {
if (i % 3 == 0 && i % 5 == 0) {
fizzBuzzString += "Fizz Buzz" + ",";
} else if (i % 3 == 0) {
fizzBuzzString += "Fizz" + ",";
} else if (i % 5 == 0) {
fizzBuzzString += "Buzz" + ",";
} else {
fizzBuzzString += i + ",";
}
}
fizzBuzzString = fizzBuzzString.slice(0, -1);
return fizzBuzzString;
}