Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
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();
    }
};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

Use

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

Your condition was negated (!), removing that worked (Or you could flip the comparisons around). Also, for checking whether the input is not a number I'd recommend isNaN instead of checking against an empty string, you checked against a space by accident which could get awkward and be a pain to find.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!