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 trialQasim Rashad
Front End Web Development Techdegree Student 7,837 PointsI need help with an question
It saying its an error but can't spot the error. Did I do it correctly?
var a = 10;
var b = 20;
var c = 30;
if { a > b};
else { "a is not greater than b"};
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
3 Answers
Ben Reynolds
35,170 PointsThe if/else syntax should look like this:
if (Condition To Test Goes Here) {
// this code will run if condition is true
} else {
// this code will run if condition is false
}
The commented parts of my example above are where you'll write the code for the alerts.
Agnes Demes
6,613 PointsHi there, When you use conditional .s the syntax is very important. If (condition) - using brackets around condition { code to be executed } - using curly braces around code to be executed else no condition needed as when IF statement is false this code will execute inside the curly brace{ code to be executed}
var x = 10;
var y = 20;
var z = 30;
if (x>y) {
alert("write something here");
} else {
alert("write something here");
}
Qasim Rashad
Front End Web Development Techdegree Student 7,837 PointsI figured it out thank you so much for the help!
Agnes Demes
6,613 PointsYou are almost there, only you dont need the brackets after the else condition as when the if condition is false the else condition will execute so there are only 2 possible outcomes. The quiz questions are very specific and strict. For instance when i was competing this challenge I typed "then" instead of "than" and it came back with an error. dont forget the ; semicolon after the else statement.
Qasim Rashad
Front End Web Development Techdegree Student 7,837 PointsQasim Rashad
Front End Web Development Techdegree Student 7,837 PointsI’ve put,
if (a > b) {
Alert (“a is greater than b”)} Else { alert (“a is not greater than b”)}
It popped as error, did I do something incorrectly?