Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda