0

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

Strixium
  • 1
  • 1
  • 1
    It should be `websitechoice == 1`, not `websitechoice = 1`, where the latter is setting `websitechoice` to 1 instead of comparing it to 1. – Rickard Elimää May 17 '22 at 15:51
  • 2
    Also, `while (websitechoice != 1 || 2 || 3)` is wrong. It needs to be `while (websitechoice != 1 || websitechoice != 2 || websitechoice != 3)` But don't think a `while` is needed in the first place. – 0stone0 May 17 '22 at 15:53

3 Answers3

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

Alex
  • 531
  • 1
  • 8
  • 22
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 !

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

Tripparsugo
  • 11
  • 1
  • 4