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

353
Views
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 answers
Answer question

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 Report

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 Report

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 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!