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

Question 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

The 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

Thank you. I tried it like this:

var isAdmin = false;
var isStudent = false;

if ( isAdmin ) {
    alert('Welcome administrator');
} else if (isStudent) {
    alert('Welcome student');
} else ( false ) {
  alert("Who are you?");
}

Thanks to you; I was only stuck for about 45 seconds. :)

Thank you.

this is great and thanks for the clear explanation on the issue of giving an else statement the parameters...

Thanks for your answer. It saved me a lot of time and frustration...

it worked thanks :)

Awesome! 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

Michael 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.

THX

I'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.

that's how it's suppose to be

that answer was much clearer than the video