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 trialBrynja Björk Guðmundsdóttir
12,147 PointsThey say that it's not working but I have rechecked it and I can see nothing wrong, please help me see the mistake.
So I´m doing this JavaScript code and they say that it´s not working but I can't see any mistake with the code.. can someone help me?
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
Shayne Laufenberg
4,213 PointsHey there! I've found the problem with your code. So in JavaScript and in many other languages we use if statements as a way to come up with a different outcome using different user inputs and variables. In this case it looks as if you tried to give else a condition but else cannot take any conditions as its meant to be a last resort if everything else in your statement is false. For this challenge specifically I would suggest using else but in the future if you wish to you can use "else if" and have a condition like how you have it in your file. Here is the solution to the problem hopefully it will help you better understand :)
Solution:
// Shayne Laufenberg //
// JavaScript Basics - Introducing Conditional Statements //
// Prompt User, and Store their answer //
var answer = prompt("What is the best programming language?");
// Check if User's Answer is equal to JavaScript //
if(answer === "JavaScript"){
// Alert User Message //
alert("You are correct");
}else{
// Alert User Message //
alert("JavaScript is the best language!");
}
Raja Kannan
6,590 PointsHi, there is a very small error in your code. In the else statement you no need to put any conditions. So, the parentheses following the else will show error. Try the code below,
var answer = prompt('What is the best programming language?');
if(answer === 'JavaScript'){
alert("You are correct");
}else{
alert("JavaScript is the best language!");
}