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

Michael Criste
Michael Criste
5,045 Points

Conditional Statements using Alerts. Tried numerous syntaxes. Need to move on. Please help. Thnx.

Please see challenge. Self explanatory.

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>

2 Answers

Two things:

  1. "else" cannot have a conditional statement with it. It's just else {. If you want to evaluate a second condition, you'd use else if ( /* your condition here */ ) {.
  2. !> is not a valid operator. Please see the MDN page on JavaScript expressions and operators for a list of all the operators available. If you wanted to say "A is not greater than B," you'd probably want to simply write "A is less than or equal to B," which is a <= b. Or (a bit more awkwardly, but valid), you could write !(a > b) which is like saying, "It's not true that A is greater than B."

I understand you want to move forward, but it's important to spend some time studying this stuff and getting the foundations down. It gets a lot harder than this.

Brian Pedigo
Brian Pedigo
26,783 Points

Hi Michael after looking over your code it looks to me that you have added a logic condition to your else statement. Which is not correct. The else statement doesn't require logic because when control gets to the else clause it means that none of the above logic has worked and control is going to fire the else statement.

var a = 10;
var b = 20;
var c = 30;

if(a>b){
  alert("a is greater than b");
} else {
  alert("a is not greater than b");
}

I hope this helps! vote it up if did! happy coding!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi Brian! I've changed your comment to an answer so that it can be eligible for voting and possible assignment of best answer. When leaving an answer, put it in the "Add an Answer" box instead of "comment". Comments are not eligible for voting. Thanks for helping out in the Community! :sparkles: