-1

I need to have a bunch of numbers asked by prompt, depending on the number there's a message, kind of fortune cookies, but if the user enters a word it needs to say "You typed a word, we need a number", as well as if it is a negative number it will say "negative numbers won't work here"

I know how to do everything but I still can't figure out how to display this part

let number = +prompt("choose a number for good luck")

if (number ===NaN){
document.getElementById("fortuneOutput").innerHTML = "You typed a word, we need a number" 
}
else if (number < 0){
document.getElementById("fortuneOutput").innerHTML = "negative numbers won't work here"
}

but none of them work

  • 1
    https://stackoverflow.com/questions/2652319/how-do-you-check-that-a-number-is-nan-in-javascript – epascarello Feb 04 '21 at 00:20
  • What is number? How is it defined? You should show more code. – epascarello Feb 04 '21 at 00:20
  • Please post a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), preferably as a [stack snippet](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do). – costaparas Feb 04 '21 at 00:21
  • Your code should work if you use `parseInt()` to convert the user input to a number. – Barmar Feb 04 '21 at 00:23
  • @Barmar the `+` will [convert to a number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus). – Phix Feb 04 '21 at 00:46
  • @Phix That wasn't in the question when I posted my comment. – Barmar Feb 04 '21 at 00:46
  • @Barmar good callout, I didn't see the edit. – Phix Feb 04 '21 at 00:47

1 Answers1

1

Use isNaN(), as NaN is never equal to NaN.

Also, parseInt() makes strings to ints/numbers.

0xLogN
  • 3,289
  • 1
  • 14
  • 35