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 (Retired) Making Decisions with Conditional Statements Add a Final Else Clause

Stevie Fortune
Stevie Fortune
2,677 Points

Can't get else clause to test if two variables are false

I can't get the final else clause to test whether or not both variables are false in the code challenge... any explanation would be great please..

script.js
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?'); 
}
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

4 Answers

andren
andren
28,558 Points

Else statements does not take conditions of any kind, they are automatically executed if the if/else if statements above them are not executed. That is what differentiates an else statement and an else if statement.

You could fix this by changing the final else to an else if statement (you can have as many else if statements under an if statement as you want). But you could also just remove the conditions entirely, and that is what the task wants in this case. If the first if statement does not run and the else if does not as well then both isAdmin and isStudent has to be false, therefore you don't need to check for that in the last statement in this case.

You should not write a condition in the else clause. General definition of JavaScript "if/else" statement is: if (condition) { do something } else if (condition) { do something } else { do something }

Stevie Fortune
Stevie Fortune
2,677 Points

Thanks Ronald, the actual question is "Add a final else clause to this conditional statement so that if the isAdmin variable and isStudent variables are both false an alert opens with the message "Who are you?""

This to me reads as though there needs to be a condition (In my code is, "isAdmin=== false && isStudent=== false") in the else clause to run the alert. (alert ('Who are you?');)

I know that I am incorrect, but am I in the ball park?

Oh, I see where this might have confused you Stevie.

"Add a final else clause to this conditional statement so that if the isAdmin variable and isStudent variables are both false an alert opens with the message "Who are you?"" "This to me reads as though there needs to be a condition (In my code is, "isAdmin=== false && isStudent=== false") in the else clause to run the alert. (alert ('Who are you?');)"

Think of it this way: if the first "if" statement does not run, that means automatically that isAdmin===false. If the "if else" condition does not run, that means automatically that isStudent===false, because neither of them are true. Thus you don't need to specifically add a condition which checks if both of them are false.

Stevie Fortune
Stevie Fortune
2,677 Points

Ah ha, now I understand! Thank you both for your help. Compared to HTML and CSS, I'm struggling a bit getting my head around this JS stuff... I'll get there eventually.. Thanks again guys.