Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

356
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda