Why is my isNaN not working in my if statement

Viewed 74

new to JS. In the following code, the isNaN isn't working. If you enter a number in the prompt, the FizzBuzz rules work fine. However, if you enter a random string, I expect the isNaN condition to be met. What am I doing wrong?

let number = parseInt(prompt("Enter a number"));

for(let i = 1; i < number; i++) {
  if(isNaN(number)) {
    console.log("Is not a number")
  } else if(i % 3 === 0 && i % 5 === 0) {
    console.log("FizzBuzz")
  } else if (i % 3 === 0) {
    console.log("Fizz")
  } else if (i % 5 === 0) {
    console.log("Buzz")
  } else {
    console.log(i)
  }
}

4 Answers

As @RobinZigmond correctly pointed out, when you enter an arbitrary string, number will be NaN.

For loop basically consists of:

for (Initialization, Condition, Increment)

Your condition: let i = 1 is run one time, then your condition: i < number is checked, in this case 1 < NaN which is clearly false, as the condition here is not met, you never enter your for loop.

One way to do what you're trying to do is:

let number;
while (!isNaN(number)) {
   number = parseInt(prompt("Enter a number"));
}

for(let i = 1; i < number; i++) {
  if(i % 3 === 0 && i % 5 === 0) {
    console.log("FizzBuzz")
  } else if (i % 3 === 0) {
    console.log("Fizz")
  } else if (i % 5 === 0) {
    console.log("Buzz")
  } else {
    console.log(i)
  }
}

let userInput = prompt("Enter a number");
if(isNaN(userInput)) {
  console.log("Is not a number")
} else {
  let number = parseInt(userInput);

  for(let i = 1; i < number; i++) {
    if(i % 3 === 0 && i % 5 === 0) {
      console.log("FizzBuzz")
    } else if (i % 3 === 0) {
      console.log("Fizz")
    } else if (i % 5 === 0) {
      console.log("Buzz")
    } else {
      console.log(i)
    }
  }
}

This works as expected (putting the isNaN check outside of the loop.

Move your first if statement out of the loop. It never gets read, because the expression parseInt(string) < number will always return false.

let number = parseInt(prompt("Enter a number"));

if (isNaN(number)) {
  console.log("Is not a number")
}

for (let i = 1; i < number; i++) {
  if (i % 3 === 0 && i % 5 === 0) {
    console.log("FizzBuzz")
  } else if (i % 3 === 0) {
    console.log("Fizz")
  } else if (i % 5 === 0) {
    console.log("Buzz")
  } else {
    console.log(i)
  }
}

When parseInt can't parse the string to an integer if will return NaN. In your for loop you use number in the condition i < number. If NaN is compared to a number using a comparison operator such as < or > the result will always be false and the loop will not run at all, this is why nothing happens when you enter a random string.

To get the result you are expecting You could put the isNaN check outside the for loop and only run the loop if number is not equal to NaN.

let number = parseInt(prompt("Enter a number"));
if (isNaN(number)) {
  console.log("Is not a number")
} else {
  for(let i = 1; i < number; i++) {
    if(i % 3 === 0 && i % 5 === 0) {
      console.log("FizzBuzz")
    } else if (i % 3 === 0) {
      console.log("Fizz")
    } else if (i % 5 === 0) {
      console.log("Buzz")
    } else {
      console.log(i)
    }
  }
}

Related