Home > OS >  how to check if prompt value is a NaN and pass that information?
how to check if prompt value is a NaN and pass that information?

Time:01-18

I've just started learning how to code in JS and I wanted to make a program that greets the user after he fills the information needed age and name.

I added a loop that checks if provided data typed in the prompt is a number but it seems to go on infinitely, like the value was always wrong and the loop looped itself over and over even if the value was right (of course after I firstly typed in the wrong value).

The best part is when I display the typeof value it shows it's right and wrong at the same time.

alert("Hi this site is only accsesable by pepole above an age of 18")
let usersAge = prompt("How old are you?");
usersAge = Number(usersAge);

while (Number.isNaN(usersAge)) {
  let usersAge = prompt("type in the correct value?");
}

if (usersAge >= 18) {
  let userName = prompt("cool,what is ur name")
  toString(usersAge)
  console.log("wassup"   " "   userName   " "   "with age of"   " "   usersAge)
} else {
  console.log("sorry ur age is to low for us to display this website")
};

console.log(typeof usersAge)
console.log(usersAge)

Console output:

sorry ur age is to low for us to display this website
"number"
NaN

CodePudding user response:

The first issue is because you're redefining usersAge within the while block, which affects the outcome of that loop. Remove the let keyword there.

The other issue is that you're comparing strings to integers in some cases. Cast all values to the same type before comparison.

With those issues addressed your code works:

alert("Hi this site is only accsesable by pepole above an age of 18")
let usersAge = parseInt(prompt("How old are you?"), 10);

while (Number.isNaN(usersAge)) {
  usersAge = parseInt(prompt("type in the correct value?"), 10);
}

if (usersAge >= 18) {
  let userName = prompt("cool,what is ur name")
  toString(usersAge)
  console.log("wassup"   " "   userName   " "   "with age of"   " "   usersAge)
} else {
  console.log("sorry ur age is to low for us to display this website")
};

console.log(typeof usersAge)
console.log(usersAge)

However putting a prompt() in a potentially infinite loop is a really bad design choice. The kind which will infuriate your users and get your site blacklisted.

A better approach would be to validate the input and if it's invalid then you should give the user the choice to cancel out of the loop and leave your site. Right now you're forcing the user to enter an age before they can leave.

CodePudding user response:

Hi my friend to fix your problem you need to not use let because you use it once. I'm going to give you the right syntax for your problem :

alert("Hi this site is only accsesable by pepole above an age of 18");
  let usersAge=prompt("How old are you?");
  usersAge = Number(usersAge);
  
  while(Number.isNaN(usersAge) || usersAge === null || usersAge === '') {
    usersAge= prompt("Enter your age")
  }
{
  if( usersAge  >= 18){
    let userName = prompt("cool,what is ur name");
    toString(usersAge)
    console.log("wassup"   " "   userName   " "   "with age of"   " "   usersAge)
    
  }

  if( usersAge  < 18){
    console.log("sorry ur age is to low for us to display this website");
    
  } else{
    usersAge = prompt("type in the correct value?")
  }
}

I hope your problem was solved.

  • Related