Skip to content
Advertisement

User to select certain numbers from prompt

I’m attempting to make the user select certain numbers from a prompt, the empty input returns the alert message but I have no clue how to go about getting an alert message when the user chooses a number that is less than 8 or more than 128. Thank you!

var passwordLength = function () {
    //parseInt convert a string into an integer
    var charLength = parseInt(prompt("How many characters would you like your password to be? Please choose a number from (8-128)"));

    if (!(charLength > 8 || charLength < 128 || charLength === " ")) {
        alert("Please choose a number from (0-128)");
        return passwordLength();
    }
};

Advertisement

Answer

Your conditional is messed up.

It needs to be either

!(charLength > 8 || charLength < 128 || charLength !== "")

or

(charLength < 8 || charLength > 128 || charLength === "")

Note also that, if anything, you’d need to use empty strings, not strings with a space in it. But I see no real reason for that: parseInt will either return a number or NaN, so, as LW001 suggests, you may want to use isNaN instead.

Advertisement