-2
function counter(numOne, numTwo) {
  for (let i = 0; i <= 100; i++) {

    if (i % numOne === 0) {
      console.log("Fizz");
    }
    if (i % numTwo === 0) {
      console.log("Buzz");
    }
    if (i % numOne === 0 && i % numTwo === 0) {
      console.log("FizzBuzz");
    }
    else if (i <= 100 && i !== i % numOne === 0 || i !== i % numTwo === 0) {
      console.log(i);
    }
  }
}

counter(3, 5);

For the else if loop, it should console.log all numbers that are <=100, but are not i % numOne === 0 and i % numTwo === 0. So why are only Fizz, Buzz, and FizzBuzz showing up in the output?

Arber Sylejmani
  • 2,078
  • 1
  • 16
  • 20
Cutecat42
  • 77
  • 8
  • 2
    Can you explain this: (i <= 100 && i !== i % numOne === 0 || i !== i % numTwo === 0)? – Arber Sylejmani May 24 '20 at 23:48
  • i !== i % numOne === 0 results in: false % numOne === 0, which is NaN === 0, and that is never true :) – Arber Sylejmani May 24 '20 at 23:48
  • 1
    try changing the else if condition to `(i <= 100 && i % numOne !== 0 && i % numTwo !== 0) ` as the condition you have written will not serve the purpose. As stated in comment by @ArberSylejmani – Dexter May 24 '20 at 23:49
  • 1
    Welcome to Stack Overflow. Note that there are already (a lot of) FizzBuzz questions/answers available on Stack Overflow. Instead of posting yet another FizzBuzz question it would have better to first search for existing questions here, and only add a new (your) question if all of the already existing questions could not help you finding an answer (yourself). (In that case you would link to the existing question and explain what's the difference). Have a look at [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – Ivo Mori May 25 '20 at 00:12

1 Answers1

3

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

Arber Sylejmani
  • 2,078
  • 1
  • 16
  • 20
  • So I basically just had to tweak the else if, but to simplify it even more, I could just have created them in variables. That is great to know. I have a long ways to go before knowing even intermediate javascript! Thank you! – Cutecat42 May 25 '20 at 00:00
  • 2
    There are many ways to do a FizzBuzz challenge, but the reason why I assigned them to variables is because when names are more meaningful, it's easier to understand the logic when you have the same (or reverse) condition used in more than one place – Arber Sylejmani May 25 '20 at 00:02
  • Actually to correct a small part from my answer, only when numOne is 0, so (false % 0) results in a NaN, any other number equals 0, but I wouldn't trust a math result between a boolean and a number :) You'll get into weird bugs territory! – Arber Sylejmani May 25 '20 at 00:07
  • 1
    @Thomas you're right, but I was addressing the issue she was asking about, which was the else expression. I'll update the answer to show the correct FizzBuzz behaviour so it doesn't confuse or mislead anyone. – Arber Sylejmani May 25 '20 at 06:56
  • @Cutecat42 please, see how you'd approach this FizzBuzz without the need of your last "else if" – Arber Sylejmani May 25 '20 at 06:58