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 trialidriss Elouilani
Courses Plus Student 1,232 Pointsthe else Question doesn't go through
the else question doesn't work 1 var answer = prompt ('What is the best programming language?'); 2 if (answer === "JavaScript")alert("You are correct") else{alert("JavaScript is the best language!");
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>
5 Answers
Jennifer Nordell
Treehouse TeacherHi there! You've introduced a syntax error and started the else
statement before it was really prepared. So if you run your code in the console you will get (depending on your browser) an error that looks something like: Uncaught SyntaxError: Unexpected token else
.
There are a couple of ways to solve this. If you like your if and else on one line you can simply insert a semicolon after the first alert statement:
if (answer==="JavaScript")alert("You are correct");else{alert("JavaScript is the best language")};
Personally, I prefer to do something like this so it looks more obvious what's going on:
if (answer==="JavaScript") {
alert("You are correct");
} else {
alert("JavaScript is the best language");
}
But either will work. Hope this helps!
Jonathan Grieve
Treehouse Moderator 91,253 PointsHello Idriss,
It looks like you haven't provided the curly braces for your "truthy" block but you have for the "else" block.
Also you should terminate each statement, (which remember is different from an expression) with a semi colon. That's like punctuation (a period) for closing a sentence.
Try adding in the curlybraces in the appropriate places for the true part of the condition. :-)
i.e.
if () {} else {}
idriss Elouilani
Courses Plus Student 1,232 PointsThank u Jenniffer
idriss Elouilani
Courses Plus Student 1,232 PointsThank U Jennifer
idriss Elouilani
Courses Plus Student 1,232 Pointsthank u Jonathan