0

I am trying to write a small program, and I have 3 conditions for the program to proceed.

  1. prompt user input again if hour > 12
  2. prompt user input again if hour < 1
  3. prompt user input again if hour === NaN (I am stuck with the 3rd condition)

Here is the code I wrote to cope with 1st 2 conditions and unable to do the 3rd.

let hour = parseInt(prompt('Enter Hour'));

while (hour > 12 || hour < 1) {
    hour = parseInt(prompt('Enter Valid Hour'));
}

i am pretty new to coding. Help is appreciated.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Ali Ahmed
  • 155
  • 9
  • 3
    You're looking for `isNaN(hour)` – Pointy Apr 13 '21 at 12:42
  • 2
    May I suggest you change the title, as your question does not seem to be related to the `while` loop but it is actually about how to check for `NaN` values? – secan Apr 13 '21 at 12:48

2 Answers2

1
let hour = parseInt(prompt('Enter Hour'));

while (hour > 12 || hour < 1 || isNaN(hour)) {
    hour = parseInt(prompt('Enter Valid Hour'));
}
Pawan Sharma
  • 1,842
  • 1
  • 14
  • 18
1

You need to use isNaN()

let hour = parseInt(prompt('Enter Hour'));

while (hour > 12 || hour < 1 || isNaN(hour)) {
    hour = parseInt(prompt('Enter Valid Hour'));
}
Rojo
  • 2,749
  • 1
  • 13
  • 34