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

354
Visualizações
While loop not displaying correct result Javascript

I'm working on a project for one of my classes where I need to use the users input to redirect them to a website. It requires that the users choice be validated using a loop, so I've chosen to use a while loop to check for if the users input differs from what it should be and if it is, the user is prompted to re-enter their answer. Here's the code:

    var websitechoice;
    websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))

    while (websitechoice != 1 || 2 || 3) {
        alert("you input an incorrect value")
        websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))
    }
    if (websitechoice = 1) {
        alert("you chose 1")
    }
    else if (websitechoice = 2) {
        alert("you chose 2")
    }
    else {
        alert("you chose 3")
    } 

So far it was just a quick mock up I made to check if it would work, but every time I try and run it, I always get back "you input an incorrect value" even when inputting 1, 2, or 3, and so far nothing I've tried had differed the results. if anyone could help I'd really appreciate it, thanks

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You're using a definition in the if statement, in that you're telling the code that websitechoice is now equal to that number. You should use a comparator like so

 if (websitechoice == 1) {
        alert("you chose 1")
    }
    else if (websitechoice == 2) {
        alert("you chose 2")
    }
    else {
        alert("you chose 3")
    } 

For your while statement, you should change it to and statement, because you only want to run the code when it's not equal to one and not eqaul to two and not equal to three

while (websitechoice != 1 && websitechoice != 2 && websitechoice != 3) {
        alert("you input an incorrect value")
        websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))
    }

You could also just say, if websitechoice > 3 then do the alert process

about 4 years ago · Juan Pablo Isaza Relatório

0

There are many flaws in your code. First of all, you should use AND operator instead of OR operator. Also, you need to break the comparison as beloe demonstrated.

Secondly, you have used assignment operator ( = ) in the if else statement. You need to use comparison operator ( == or === ) to compare otherwise it will return 1 everytime.

var websitechoice;
        websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))

        while (websitechoice !== 1 && websitechoice !== 2 && websitechoice !== 3) {
            alert("you input an incorrect value")
            websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))
        }
        if (websitechoice === 1) {
            alert("you chose 1")
        }
        else if (websitechoice === 2) {
            alert("you chose 2")
        }
        else {
            alert("you chose 3")
        } 

Also, it will be easy to debug if you put your code in the snippet. Seems like you are quite new to developer community. Welcome to coder community !

about 4 years ago · Juan Pablo Isaza Relatório

0

The != has precedence over the || operator 1 so:

websitechoice != 1 || 2 || 3

will always evaluate to:

(websitechoice != 1) || 2 || 3

which is not what you want...

another issue is that:

websitechoice = 1

is an assignment not a comparison.

To fix your code without changing its structure:

    var websitechoice;
websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))

while (websitechoice !== 1 && websitechoice !== 2 && websitechoice !== 3) {
    alert("you input an incorrect value")
    websitechoice = parseInt(prompt("Which website would you like to go to? \n 1. google \n 2. gmail \n 3. youtube"))
}
if (websitechoice === 1) {
    alert("you chose 1")
}
else if (websitechoice === 2) {
    alert("you chose 2")
}
else {
    alert("you chose 3")
}

A cleaner way to check if your input is one of a list of values would probably be:

[1,2,3].includes(websitechoice)

which is cleaner and can scale easily to more values.

I've also replaced the == operators with ===, don't use == in JS unless you know what you are doing. 2

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