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
Tim McEwan
19,537 PointsQuestion on a Javascript challenge
Add a final else clause to this conditional statement so that if the isAdmin variable and student variables are both false an alert opens with the message "Who are you?"
This is what i am putting and am still striking out..
var isAdmin = false;
var isStudent = false;
if ( isAdmin ) {
alert('Welcome administrator')
} else if (isStudent) {
alert('Welcome student')
} else (isAdmin === false && isStudent === false) {
alert("Who are you?");
}
6 Answers
Michael Hulet
47,914 PointsThe thing with else statements is that you can't specify parameters for when they should fire. They're kinda like the default case in switch(){case} statements. If you remove everything next to the else keyword and the opening bracket, it should work. Try this:
var isAdmin = false;
var isStudent = false;
if ( isAdmin ) {
alert('Welcome administrator');
} else if (isStudent) {
alert('Welcome student');
} else {
alert("Who are you?");
}
On a side note, I prettified your code for you. If you want to learn how, you should read the Markdown Cheatsheet that's under the box where you would enter a reply to this. For a specific example, you could hit the Edit button under your original post and look at how I formatted your code
Tim McEwan
19,537 Pointsit worked thanks :)
Michael Hulet
47,914 PointsAwesome! Also, I just noticed that you're missing a couple semicolons. Namely, right after your first two calls to alert(). I updated the code in my first reply to put them where they're supposed to be
Tim McEwan
19,537 PointsMichael thanks!!! I noticed that too. The weird thing is that those semicolons were not added by the challenge itself. I only had answer the last else statement. But i am usually a stickler for adding in semicolons.
Raymundo Figueroa
Front End Web Development Techdegree Student 25,606 PointsTHX
Marcin Robert Kaźmierczak
33,570 PointsI've just passed the code challenge, but there is a problem with tasks counter. We have 3 tasks to do , but the counter is showing only 1 of 1 on each of them.
Brandon Provo
4,188 Pointsthat's how it's suppose to be
David Pinner
7,039 Pointsthat answer was much clearer than the video
Ryan Roybal
4,682 PointsRyan Roybal
4,682 PointsThank you. I tried it like this:
Thanks to you; I was only stuck for about 45 seconds. :)
Thank you.
claudius mainja
9,661 Pointsclaudius mainja
9,661 Pointsthis is great and thanks for the clear explanation on the issue of giving an else statement the parameters...
Emil Nastase
4,396 PointsEmil Nastase
4,396 PointsThanks for your answer. It saved me a lot of time and frustration...