[Solved] Why is my FizzBuzz code not outputting correctly?


Ok, I didn’t want to write an answer but since you’re new here, I’ll put this in a more meaningful way:

function counter(numOne, numTwo) {
  for (let i = 0; i <= 100; i++) {
    const isFizz = i % numOne === 0
    const isBuzz = i % numTwo === 0

    if (isFizz && isBuzz) {
      console.log("FizzBuzz");
    }
    else if (isFizz) {
      console.log("Fizz");
    }
    else if (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:

  1. i !== i can never be true, it’s the same value, it’s always i === i or in your case false
  2. 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

5

solved Why is my FizzBuzz code not outputting correctly?