Why Is My Fizzbuzz Code Not Outputting Correctly?
function counter(numOne, numTwo) { for (let i = 0; i <= 100; i++) { if (i % numOne === 0) { console.log('Fizz'); } if (i % numTwo === 0) { console.log
Solution 1:
Ok, I didn't want to write an answer but since you're new here, I'll put this in a more meaningful way:
functioncounter(numOne, numTwo) {
for (let i = 0; i <= 100; i++) {
const isFizz = i % numOne === 0const isBuzz = i % numTwo === 0if (isFizz && isBuzz) {
console.log("FizzBuzz");
}
elseif (isFizz) {
console.log("Fizz");
}
elseif (isBuzz) {
console.log("isBuzz")
}
else {
console.log(i);
}
}
}
counter(3, 5);
In your example, you had:
i !== i % numOne === 0
as stated above, there are two issues here:
- i !== i can never be true, it's the same value, it's always i === i or in your case false
- Since the above is false, you'll have a math equation of: false % numOne this will result in a NaN and NaN does not equal 0
Hope this and the comments above helps understand your issue
Post a Comment for "Why Is My Fizzbuzz Code Not Outputting Correctly?"