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 trialHeidi Toft
1,894 PointsConditional Statement
When I get to adding the else statement and the second alert it does not work. I have looked at the code and I cannot figure out what is missing or wrong. Please help. Thank you, Heidi
var answer = prompt("What is the best programming language?");
if (answer === 'JavaScript'); {
alert ("You are correct");
} else {
alert ("JavaScript is the best language!");
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript Basics</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
2 Answers
andren
28,558 PointsThe issue is the semicolon in this line:
if (answer === 'JavaScript'); { // <- ; does not belong there
You are not meant to put a semicolon after the conditions of an if
statement or similar type of statements. The semicolon tells JavaScript that you are done declaring the if
statement even though you have not actually provided it with the code to run yet.
If you remove that semicolon like this:
var answer = prompt("What is the best programming language?");
if (answer === 'JavaScript') {
alert ("You are correct");
} else {
alert ("JavaScript is the best language!");
}
Then your code will work.
Maxime Duhamel
7,169 PointsHi, Really a very little mistake. When you write an if statement the condition is not ended by a semi-column. You can jump right to the curly brace. Format is :
if (condition to check) {
action to execute;
other action to execute;
} else {
action to execute when none of the above conditions are true;
}
Syntax is sometime frustrating ^^
Max
Heidi Toft
1,894 PointsThank you. I figured it had to be something small but makes a big impact.
Thanks!
Maxime Duhamel
7,169 PointsYou are welcome Heidi, Keep making mistakes like this as this is the best way to think about what went wrong and solve it easier next time you run into it.
Heidi Toft
1,894 PointsHeidi Toft
1,894 PointsThank you. I think I now remember hearing about that in the video.