Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 28,992 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 28,992 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 28,992 Points

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

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 28,992 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,186 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 28,992 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 28,992 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 28,992 Points

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

Amadou Sow
Amadou Sow
6,082 Points

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

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

Amadou Sow
Amadou Sow
6,082 Points

That's the correct answer.