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 Making Decisions in Your Code with Conditional Statements Add a Final 'else' Clause

Add a final else clause to this conditional statement. If both the isAdmin and isStudent variables are false, the value

script.js
const isAdmin = false;
const isStudent = false;
let message;

if ( isAdmin ) {
  message = 'Welcome admin';
} else if ( isStudent ) {
  message = 'Welcome student';
} else ( isAdmin + isStudent === false ) { 
  message = 'Access denied';
}

10 Answers

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 38,304 Points

You don’t need and can’t have a condition on the else. The else will run if the other 2 conditions are false. So just remove the condition and it should work.

const isAdmin = false; const isStudent = false; let message;

if ( isAdmin ) { message = 'Welcome admin'; } else if( isStudent ) { message = 'Welcome student'; } ( isAdmin + isStudent === false ) { message = 'Access denied'; }

condition?

Mark Sebeck
Mark Sebeck
Treehouse Moderator 38,304 Points

You just need the else like this

} else { 
  message = 'Access denied';
}

the condition is what you had in () after the else

This works perfectly in the browser, but not in the challenge. Finally, I have realized the final DOT after "Access denied" was misplaced INSIDE the quote marks, which made all the difference. Frustrating :(. `const isAdmin = false; const isStudent = false; let message;

if ( isAdmin ) { message = 'Welcome admin'; } else if ( isStudent ) { message = 'Welcome student'; } else { message = 'Access denied.'; }`

Mark Sebeck
Mark Sebeck
Treehouse Moderator 38,304 Points

Hi Sonia. Yes in challenges strings have to be exact or they won't pass. "Access denied" !== "Access denied."

Tom Nguyen
Tom Nguyen
33,501 Points

This is what I used to pass:

script.js
const isAdmin = false;
const isStudent = false;
let message;

if ( isAdmin ) {
  message = 'Welcome admin';
} else if ( isStudent ) {
  message = 'Welcome student';
} else {
  message = "Access denied";
}

condition = ()

Thank you!

Mark Sebeck
Mark Sebeck
Treehouse Moderator 38,304 Points

You are welcome. Yes conditions are only for if and else if. Then the else will run if all conditions are false.

if (condition) 

else if (condition)

else
Shanna vick-Morris
Shanna vick-Morris
6,683 Points
const isAdmin = false;
const isStudent = false;
let message;

if ( isAdmin ) {
  message = 'Welcome admin';
} else if ( isStudent ) {
  message = 'Welcome student';
} 
{isAdmin + isStudent = false} {
  message = 'Access denied';
}

I am getting and ERROR: "Did you add an 'else' clause to the end of a conditional statement"?

Mark Sebeck
Mark Sebeck
Treehouse Moderator 38,304 Points

Hi Shanna. You need to add an else to the end of the conditional statement. :). Hope that helps

...
} else
{
  message = 'Access denied';
}

Hi I am struggling about this exact tusk right now . looks like I am doing everything alright . can you explain my mistake ( isAdmin + isStudent === false ); } else { message = 'Access denied';

Mark Sebeck
Mark Sebeck
Treehouse Moderator 38,304 Points

Hi Khatia. So the challenge gives us:

const isAdmin = false;
const isStudent = false;
let message;

if ( isAdmin ) {
  message = 'Welcome admin';
} else if ( isStudent ) {
  message = 'Welcome student';
} 

So I see what you are doing. It's a good try. You are making sure both conditions are false. But you can't conditions like that. The good news is you don't need to. If your code makes it to the else then all conditions above had to be false. So delete this part:

( isAdmin + isStudent === false );

and simply use your else. And don't forget to close your bracket at the end.

Hope this helps.

( isAdmin + isStudent === false ); } else { message = 'Access denied'; }

is this what you mean ? or I do not need to write message = ' Access denied ' ;

okay I deleted the one you told me and my code looks like this : } else message = 'Access denied'; } still does not run .

That is how everything looks like right now when I am trying to finish it .

const isAdmin = false; const isStudent = false; let message;

if ( isAdmin ) { message = 'Welcome admin'; } else if ( isStudent ) { message = 'Welcome student'; } } else { message = 'Access denied'; }

and the result is can not read property 1 null

Mark Sebeck
Mark Sebeck
Treehouse Moderator 38,304 Points

You have an extra close bracket before the else. Delete that and it should work.

const isAdmin = false; const isStudent = false; let message;

if ( isAdmin ) { message = 'Welcome admin'; } else if ( isStudent ) { message = 'Welcome student'; } else {message = 'Access denied'; }

That's the correct answer.