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

Dylan Davenport
PLUS
Dylan Davenport
Courses Plus Student 2,645 Points

Can I have multiple if else statements without needing an else if?

I have code to create a 5 question quiz. I have multiple if else statements but no else if. I don't see why I need the else if in this case. I get an error that says expected statement got keyword else. Here is my code thus far:

var questions = 5;

alert('Welcome to the JS quiz! You will be asked 5 questions, at the end depending on your success you will be awarded a crown. Good Luck!');

var question1 = prompt('What is the capital of the United States?'); if ( question1 === 'Washington DC' ) { document.write('<p><h3>CORRECT! you have ' + (questions - 1) + ' questions left, keep going.</p></h3>');

} else { document.write('<p><h3>WRONG! Sorry. Maybe the next question will be easier. ' + 'You have ' + (questions - 1) + 'questions left.</p></h3>'); }

var question2 = prompt('What is the first element on the periodic table?'); if ( question2 === 'Hydrogen' ); { document.write("<p><h3>CORRECT! Good job, but you're not out of the woods yet. " + "You have " + (questions - 1) + " questions left.</p></h3>");

} else {<<--Here is the else that it says is the problem. I don't think I need an else if here?

document.write("Sorry wrong again. 2 down " + (questions - 1) + " questions left" + " Don't give up you can do it");

}

Dylan Davenport
Dylan Davenport
Courses Plus Student 2,645 Points

I basically just want to check the conditions of the question answers and have an if else statement for each quesgtoin answer.If I add an else if I can't think of what condition I'd in the argument parenthesis.

1 Answer

Steven Parker
Steven Parker
230,274 Points

You just have a typo.

It was hard to spot without code formatting, but I finally noticed a stray semicolon on this line:

if ( question2 === 'Hydrogen' ); {   // <-- that semicolon does not belong there

For future questions, please use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down: