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 Solution

Billie Barnett
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Billie Barnett
UX Design Techdegree Graduate 17,463 Points

Trying to add conditional statement to the beginning that checks to see if quiz should run.

I am trying to add an statement to the beginning to ask the user if they would like to take the quiz.If the use says no, then the rest of the code should not run.

var answer = prompt("Are you ready for a quiz??"); if (answer.toUpperCase === "Yes" || "YEAH" || "Y" || "YEA"); { alert("Lets Get Started!!"); }

Why does the rest of the code below still run?

//assumes correct is 0 to start var correct = 0;

//Start asking quesitons for quiz var answer1 = prompt("What programming language is also the name of a gem?"); if (answer1.toUpperCase() === "RUBY"){ correct += 1; }

var answer2 = prompt("What programming language is also the name of a snake?"); if (answer2.toUpperCase() === "PYTHON"){ correct += 1; }

var answer3 = prompt("What programming language do you use to style Web Pages?"); if (answer3.toUpperCase() === "CSS"){ correct += 1; }

var answer4 = prompt("What is my loves name?"); if (answer4.toUpperCase() === "TAYLOR"){ correct += 1; }

var answer5 = prompt("Are Java and JavaScript the same programming language?"); if (answer5.toUpperCase() === "NO"){ correct += 1; }

//output results document.write("<h1>You got " + correct + " questions correct.</h1>");

//output ranking if (correct === 5) { document.write("<p><strong>You earned a gold crown!!!</strong></p>"); } else if (correct >= 3) { document.write("<p><strong>You earned a silver crown!!!</strong></p>"); } else if (correct >= 1) { document.write("<p><strong>You earned a bronze crown!!!</strong></p>"); } else document.write("<p><strong>No crown for you... Go Study</strong></p>");

3 Answers

Steven Parker
Steven Parker
231,269 Points

The conditional block in braces only encloses the alert call, so that's the only thing that won't run when the test fails.

To make the test control the whole program, the rest of the code can be moved into the conditional block (or just move that closing brace down to after the last line).

Steven Parker
Steven Parker
231,269 Points

I overlooked this the first time, but the test itself also needs a bit of work:

if (answer.toUpperCase === "Yes" || "YEAH" || "Y" || "YEA");

When you call a method, you must put parentheses after the method name (even if the method needs no arguments). And the words you match with must all be upper case themselves. Also, you need to have complete comparisons between the logic operators. And finally, there should not be a semicolon between the conditional expression and the code block:

if (answer.toUpperCase() === "YES" ||
    answer.toUpperCase() === "YEAH" ||
    answer.toUpperCase() === "Y" ||
    answer.toUpperCase() === "YEA")
Steven Barkley
Steven Barkley
8,924 Points

You don't need the semicolon following an conditional statement.

if() { }

or

if(){ }

is the standard form.

The rest of the code runs because it is not nested with the first conditional ... see example var stevenLikesGuys = True;

if ( stevenLikesGuys) { alert("Kiss Barry."); // No semicolon is needed after the conditonal and this nested statement will execute } else { alert("Kiss Barbara."); // This will not nest your code in an else and if the first condition is true it wont execute!! }

horaciomontes2
horaciomontes2
907 Points

you are finishing the if after the lets get started alert