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 trialMichael Criste
5,045 PointsConditional Statements using Alerts. Tried numerous syntaxes. Need to move on. Please help. Thnx.
Please see challenge. Self explanatory.
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");
}
<!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
Eric Butler
33,512 PointsTwo things:
- "else" cannot have a conditional statement with it. It's just
else {
. If you want to evaluate a second condition, you'd useelse if ( /* your condition here */ ) {
. -
!>
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 isa <= 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
26,783 PointsHi 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
Treehouse TeacherHi 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!