+ 1

please help, JavaScript question?

var num = prompt('Enter a number!'); if (num > 0) { alert('Your number is positive!') } else if (num < 0) { alert('Your number is negative') } else if (num == 0) { alert('The number is 0') } else if (num === '' || num === null) { alert('You didn\'t enter a number!') } else { alert('That is not a number!') }; The empty string isn't running in this case when I put num == 0 before the num === '' like I have here but the (num === null) is running.. When I press enter on the empty string, it says "The number is 0." It only runs when I put num == 0 after the num === ''. Does order really matter in this case?

5th Sep 2019, 7:07 PM
Ashley J.
Ashley J. - avatar
3 Réponses
+ 2
Maybe parseInt(num) == 0
5th Sep 2019, 7:35 PM
Oleh Sydorenko
Oleh Sydorenko - avatar
0
Change code to num ===0 == check value with autoconvert (empty string equaves 0 if vavue converts to INT) === check type variables, after check value
5th Sep 2019, 7:10 PM
Oleh Sydorenko
Oleh Sydorenko - avatar
0
When I changed it to num === 0 and typed in 0 in the prompt, it switched to "That is not a number!" Could it be the operator precedence maybe? the empty string works when I put the (num == 0) after the (num === '' || num === null)
5th Sep 2019, 7:21 PM
Ashley J.
Ashley J. - avatar