2

For an assignment in class we're tasked with getting user input using the prompt(). I only want the prompt to accept 1 of 3 values.

Here is my code:

ounce = prompt ( "What size would you like your drink?\n8oz, 12oz, 16oz are the available sizes." );

    while ( ounce!= "8oz" || ounce != "12oz" || ounce != "16oz")
{
        alert ( "Please select a proper size: 8oz, 12oz, 16oz" );
        prompt ( "What size would you like your drink?\n8oz, 12oz, 16oz are the available sizes." );
}

My intention is that when the user enters anything else but 8oz, 12oz, or 16oz it will tell them that that size is not available and them prompt them asking for their size again until they enter in one of those values. However my code as it stands is not working. Even if I enter in 8oz it says that is incorrect and asks me for my size again. I am a beginner in a beginner level course as you can tell so any help is appreciated.

2
  • 1
    || means it's true if it meets ANY of those conditions. It can't equal all three at once, right? Commented Dec 5, 2013 at 2:49
  • Try && instead of ||. Also, you're not resetting the ounce variable on the second call to prompt. Commented Dec 5, 2013 at 3:08

3 Answers 3

2

You need to use AND condition... you need to enter the loop only if the entered value is not one of the three, if you use OR then even if you enter a valid value two of the comparison operation will be turn out to be true... like if you enter 8oz then both ounce != "12oz" and ounce != "16oz" will be true.. since OR operation will return true of any one of the condition turn out to be true it will enter the loop.

If you use AND condition then the loop will be executed only if all the three conditions are met, so if you enter a valid value like 12oz then the condition ounce != "12oz" will return false resulting in the overall condition to fail thus exiting the loop.

ounce = prompt ( "What size would you like your drink?\n8oz, 12oz, 16oz are the available sizes." );

while ( ounce!= "8oz" && ounce != "12oz" && ounce != "16oz")
{
        alert ( "Please select a proper size: 8oz, 12oz, 16oz" );
        ounce  = prompt ( "What size would you like your drink?\n8oz, 12oz, 16oz are the available sizes." );
}

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

You also need to set ounce in the second prompt.
Would you mind explaining the logic behind using the AND? I read it as: "While ounce does not equal 8oz and ounce does not equal 12oz and ounce does not equal 16oz" It works, but if ounce is equal to 8oz, how can it then also be equal to 12oz and 16oz.
@user3068364 just added an example
0

The simple answer is to use AND instead of OR but I think it's worth to explain why that is.

What you're seeing here is an application of De Morgan's theorem which states:

!(A && B) === !A || !B

!(A || B) === !A && !B

So, if you do:

ounce!= "8oz" || ounce != "12oz" || ounce != "16oz"

what you're saying is:

! ( ounce == "8oz" && ounce == "12oz" && ounce == "16oz" )

which is never true because ounce can not be "8oz" and "12oz" and "16oz" all at the same time.

What I believe you want instead is:

! ( ounce == "8oz" || ounce == "12oz" || ounce == "16oz" )

which when we apply De Morgan's transformation we get:

ounce != "8oz" && ounce != "12oz" && ounce != "16oz"

Comments

0

As a side note, this is a perfect situation for a do-while.

var ounce = undefined;

do {
    alert("Please select a proper size: 8oz, 12oz, 16oz");
    ounce = prompt("What size would you like your drink?\n8oz, 12oz, 16oz are the available sizes.");
} while ( ounce != "8oz" && ounce != "12oz" && ounce != "16oz");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.