-3

When I put Character in prompt the character is printed...

var myNumber = prompt("What is your Number");
var myName = prompt("What is your name");
if (NaN != myNumber) {
  document.write("My Number is " + myNumber + " ");
} else {
  document.write("This Number is NaN ")
}
document.write("& My Name is " + myName);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 3
    Using `===` with `NaN` always results in false, and `!==` always results in true. If you want to check for `NaN`, you need to use `Number.isNaN(myNumber)` – Nicholas Tower Dec 31 '22 at 16:41
  • 1
    `prompt()` either returns a string or `null` hence `if(NaN != myNumber)` will always be true. Also comparing anything against `NaN` does not test if "anything" is a number or can be converted into a number. – Andreas Dec 31 '22 at 16:43
  • @NicholasTower There's no `===` or `!==` in the question. – Andreas Dec 31 '22 at 16:43
  • 1
    @NicholasTower _"If you want to check for NaN..."_ - Which doesn't make any sense in this case – Andreas Dec 31 '22 at 16:44
  • @Andreas The same is true for `==` and `!=` – Bergi Dec 31 '22 at 16:54
  • @Bergi Yes but it doesn't really help (OP) to talk about something that isn't in the question... – Andreas Dec 31 '22 at 17:00
  • I think the OP supposes (understandably, but incorrectly): 1. `NaN` means "not a number" 2. `!=` means "is not" 3. so `x != NaN` means "x is not not a number" 4. Therefore, x is a number. Understandable, but the way out of the fog is to read the doc about NaN. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN). It is not the set of all non-number things, and `!=` and `==` are not expressions of set non-membership or membership. – danh Dec 31 '22 at 17:22

2 Answers2

0

First to all i'll check if the actual myNumber is an actual number

if (!myNumber || isNaN(+myNumber)) {
   console.log("This Number is NaN ")
 } else {
   console.log("My Number is " + myNumber + " ");
 }
0

You have to check if the number is a valid number, every time the user submits the prompt.

Using a while loop is also a very good idea.

let myNumber = parseInt(prompt('Enter a number'));
const myName = prompt('Enter your name');

while (isNaN(myNumber)) {
    myNumber = parseInt(prompt('Please enter a valid number!'));
};

console.log(`myNumber is ${myNumber}`);
console.log(`myName is ${myName}`);

The while loop will continuously check to see if the input from the prompt can be converted into a number, without yielding NaN after the parseInt() function.

The string literals are just an easier way to concatenate the strings, especially when talking about using numbers and "+". It is simply a cleaner way. However both ways are possible.

  • Also pay attention to when the variable declaration requires you to use the parseInt() function as prompt() will always return the input as a string. – Brendan Price Dec 31 '22 at 17:34
  • Why the `while` loop? Why the template literals? – Andreas Dec 31 '22 at 17:37
  • The while loop will continuously check to see if the input from the prompt can be converted into a number, without yielding NaN after the parseInt() function. The string literals is just an easier way to concatenate strings and commonly catches me out, especially when talking about using numbers and "+". It is simply a cleaner way. Both ways are possible. – Brendan Price Dec 31 '22 at 18:12