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 trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Using Comparison Operators

Cristian Brownlee
Cristian Brownlee
8,061 Points

Conditional statement code challenge

Add a conditional statement that tests if the value in the variable a is greater than the value in variable b. If it is, pop up an alert with the message 'a is greater than b'; also add an else clause that pops up the message 'a is not greater than b'.

I can't for the life of me see where I've gone wrong.

Keep getting this message; "Bummer! There was an error with your code: SyntaxError: Parse error"

~Help!

script.js
var a = 10;
var b = 20;
var c = 30;

if ( a > b  ) {
  alert('a is greater than b')
} else ( a < b ) {
  alert('a is not greater than b')
}
index.html
<!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

Cristian Brownlee
Cristian Brownlee
8,061 Points

Figured it out. Apparently it's the curly brackets after 'else' that caused the problem, which is strange considering in the video just prior it teaches to use them. Example:

var answer = prompt('What programming language is the name of a gem?');
if ( answer.toUpperCase() === 'RUBY' ) {
  document.write("<p>That's right!</p>")
} else {
  document.write("<p>Sorry, that's wrong.</p>")
}

This works just fine, yet what I done in the challenge doesn't. Weird.

Julien riera
Julien riera
14,665 Points

Hello,

Delete your parenthesis after your 'else'. It should work.

You don't need to specify another condition as if ... else only implies one statement. If you need to use several conditions you won't use an 'if ... else' clause, but a 'if ... else if' clause.

You'll get to that very soon. :)

Cristian Brownlee
Cristian Brownlee
8,061 Points

Thanks Julien, I deleted the parenthesis after 'else' as suggested but I'm still getting the same error.

Julien riera
Julien riera
14,665 Points

Ow there is something else I didn't notice : move your ' as it has to wrap the text without your variables. (Here a and b shouldnt be wrapped by the quotes you're using (') !

Cristian Brownlee
Cristian Brownlee
8,061 Points

Thanks Julien, but the a and b did in fact need to be wrapped in the quotes as per the instructions I pasted at the top of the discussion.