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
Julie Roadnight
2,856 PointsCode challenge Conditional Statements JavaScript
Hi community
Can anyone help? I'm working my way through JavaScript and I've come to a code challenge on conditional statements. I don't understand why this is wrong. Any suggestions welcome.
The question is; 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 have; var a = 10; var b = 20; var c = 30;
if ("var a" > "var b") } window.alert("a is greater than b"); } else { window.alert("a is not greater than b"); }
Thank you
3 Answers
Ben Schroeder
22,818 PointsA few things:
- When referencing a variable that's already been declared, you only need to refer to it by name, and not with the "var" keyword or quotations. So instead of "var a" > "var b", all you need to write is a > b.
- The bracket you wrote after your first conditional is facing the wrong way.
if (a > b) {
window.alert("a is greater than b");
} else {
window.alert("a is not greater than b");
}
Hope that helps!
Zach Meyer
2,384 PointsLooks like your brackets are turned around in the first statement. But also as Ben mentioned, you just need to refer to the variables by name (a, b, or c) rather than ("var a"). If you add quotes around these variables, JS will think that you are passing in the literal text "var b," rather than the value you stored for b.
Did you mean:
var a = 10;
var b = 20;
var c = 30;
if (a > b) {
window.alert("a is greater than b");
} else {
window.alert("a is not greater than b");
}
Julie Roadnight
2,856 PointsThank you so much for your help and answers!
Riccardo Venturini
10,386 PointsRiccardo Venturini
10,386 Points"var a = 10" you use it only to create the variable which you did right. Now to call it you will need to use only "a" with out var. so:
var a = 10; var b = 20; var c = 30; if(a > b){ window.alert("a is greater than b");}else{ window.alert("a is not greater than b");}
hope this helps!