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

Ben Brenton
Ben Brenton
266 Points

My JS isn't working!!

Below I have posted some JS I am working on for a personal project, but NONE of it is executing at all. I am fairly new to the language but I cannot figure out where I might have gone wrong (I'm sure it is a syntax error somewhere). Can anyone spot the problem?

var userName = prompt("Welcome to Quizmaster! What's your name?");
    document.write("<h2>Hello, " + userName + "!</h2>");
var startOp = prompt("Are you ready to begin? yes[y] or no[n]?");
if (startOp.toUpperCase() === "YES"|| startOp.toUpperCase() === "Y") {
    document.write("<h3>Let's get started!</h3>");
} else (startOp.toUpperCase() === "NO" || startOp.toUpperCase() === "N") {
    document.write("<h3>Thanks for playing. Goodbye.</h3>");
} 

2 Answers

Hi Ben,

The problem is because else doesn't use a conditional statement - else will execute automatically if the above if / else if statements are false.

if (conditionIsTrue) {
  // do this
} else {
  // do this instead
}

Hope this helps,

Cheers

I think what you want to use is an else if instead of an else

if (startOp.toUpperCase() === "YES"|| startOp.toUpperCase() === "Y") {
    document.write("<h3>Let's get started!</h3>");
} else if (startOp.toUpperCase() === "NO" || startOp.toUpperCase() === "N") {
    document.write("<h3>Thanks for playing. Goodbye.</h3>");
} 
Ben Brenton
Ben Brenton
266 Points

Thank you Robert Richey I actually discovered this not long after posting this up so feel quite stupid now. I know 99.9% of errors like this are syntax-related so not too surprised by the outcome. Thanks anyway, I'll give you best answer for your speedy reply!