Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,136 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: