Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Jonathan Jackson
Jonathan Jackson
2,716 Points

making a quiz; why isn't my else condition executing when I input an invalid answer?

var question1 = prompt( questionNumber + ":\nWhat is the estimated air speed velocity of an unladen Swallow?\nA) 25 mph  B) 12 mph C) 50 mph D) \"I don't know that!\"");
            if ( question1.toUpperCase() === "A" ) {
                questionNumber += 1;
                q1answer = true;
                console.log("Q1: Correct");
                quizTotal += 1;
            }
            else if ( question1.toUpperCase() === "B" || "C" || "D" ) {
                questionNumber += 1;
                console.log("Q1: Incorrect");
            }
            else {
                questionNumber += 1;
                console.log("Q1: Error");
            }

I know that I'm taking this assignment way further than I need to be and using way more variables than I need to be using, but I'm doing that on purpose. I want the practice.

My question is due to the fact that when I input anything outside of "a" "b" "c" or "d", I don't get a "Q1: Error" message in the console. Instead, I get a "Q1: Incorrect."

Can anyone help? I don't see what I'm doing wrong and why it's not executing properly.

2 Answers

Jeff Lemay
Jeff Lemay
14,268 Points

Try wrapping these values in parantheses ("B" || "C" || "D"). Working demo:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
    var questionNumber = 1;
    var question1 = prompt( "1:\nWhat is the estimated air speed velocity of an unladen Swallow?\nA) 25 mph B) 12 mph C) 50 mph D) \"I don't know that!\"");
    if ( question1.toUpperCase() === "A" ) { questionNumber += 1; q1answer = true; console.log("Q1: Correct"); quizTotal += 1; } 
    else if ( question1.toUpperCase() == ("B" || "C" || "D") ) { questionNumber += 1; console.log("Q1: Incorrect"); } 
    else { questionNumber += 1; console.log("Q1: Error"); }
</script>

NOTE: the questionNumber variable was added just so this demo works. I'd imagine you set it elsewhere in your code.

Wrapping "B" || "C" || "D" in parentheses will NOT fix this.

The parentheses will cause the expression inside them to be evaluated first, which will evaluate to the string"B", since it is considered truthy.

So the conditional will effectively become question1.toUpperCase() == "B", and only check for that. An answer of "C" or "D" would then go to the else.

As Dave mentions in the video, if you use the AND (&&) or OR (||), you need to have complete conditionals on either side.

So:

else if ( question1.toUpperCase() === "B" || "C" || "D" ) {

... needs to be:

else if ( question1.toUpperCase() === "B" || question1.toUpperCase() === "C" || question1.toUpperCase() === "D" ) {

If you don't do that, then || "C" || "D" will actually always evaluate to true, since a non-empty string is considered truthy. That's why you never got to the else.