简体   繁体   中英

Why is typeof x never 'number' when x comes from the prompt function?

I'm having trouble getting the first function (below) to work correctly. I want it to ask for the age of the user with two possible outcomes. If the user enters the correct value (ie an positive number) it should return the the age. On the other hand, if the user enters an incorrect value (string, null, undefined, negative number), it should display an alert message, and have the user repeat the process until a correct value is entered and returned.

function age_of_user() {
    let age_entered = prompt("Enter Your Age:"); 
    while (typeof age_entered !== "number" || age_entered < 0) {
       alert("You entered an incorrect value. Please enter correct age.");
       age_entered = prompt("Enter Your Age:");
    }   
return age_entered;
}

function confirm_age() {
    let age = age_of_user();
    if (age < 18) {
        alert("Sorry! You need to be an adult to view content.");
    }
    else {
        alert("Welcome to our site.");
    }
}

confirm_age();

As mentioned in the comments, the prompt() function always captures the input as a string, even when the input is a valid number. To check if it's a number, you can try to parse the returned string with parseInt(age_entered) (or parseFloat if you want to allow non-integer ages, although that'd seem odd to me), and if you get back a number, the input is good - if you get back NaN , it wasn't valid.

Here's your script updated based on this understanding:

function age_of_user() {
    let age_entered = parseInt(prompt("Enter Your Age:")); 
    while (Number.isNaN(age_entered) || age_entered <= 0) {
       alert("You entered an incorrect value. Please enter correct age.");
       age_entered = parseInt(prompt("Enter Your Age:"));
    }   
return age_entered;
}

function confirm_age() {
    let age = age_of_user();
    if (age < 18) {
        alert("Sorry! You need to be an adult to view content.");
    }
    else {
        alert("Welcome to our site.");
    }
}

confirm_age();

The other answers are showing you that prompt() (almost) always returns a string. You'll need to parseInt the response before you can check it for your age range. But I think your while-loop conditional is throwing you off. Also you need to parseInt() on the prompt a second time, inside your while loop. Try it like this:

let age_entered = prompt("Enter Your Age:");
age_entered = parseInt(age_entered); 

while (age_entered <= 0 || Number.isNaN(age_entered)) {
   alert("You entered an incorrect value. Please enter correct age.");
   age_entered = prompt("Enter Your Age:");
   // do parseInt again
   age_entered = parseInt(age_entered); 
}

Notice we use Number.isNaN(age_entered). This is a more robust way to determine if a value is a number than comparing with typeof. See this doc here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN

prompt() is always returning a string,

Try parseInt(prompt("Enter Your Age:")) .

It's returning a string, and parseInt will save you:

... 
  let age = parseInt(age_of_user());    
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM