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

Yu-Che Hung
PLUS
Yu-Che Hung
Courses Plus Student 10,607 Points

javascript basics

[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?"]

first, we assign inAdmin & inStudent [false] at the begining of the code, right? so both variables should be false.

then, in my recognition ,the following condition statement should stop right at the first

condition and pop out an alert "welcome admin".

but it ends up poping out an alert "who are you?"

WHY? I got stuck here.

thank you for anyone who helps me!

script.js
var isAdmin = false;
var isStudent = false;

if ( isAdmin ) {
    alert('Welcome administrator');
} else if (isStudent) {
    alert('Welcome student');
} else{
  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>

1 Answer

andren
andren
28,558 Points

You seem to be confused about how if statements work. If statements execute if the condition you provide it is (or evaluates to) the boolean true, as you say yourself isAdmin and isStudent is set to false, which is the opposite of true. So neither the if statement or else if statement is triggered.

Tijo Thomas
Tijo Thomas
9,737 Points

@andren is correct. The reason the loop triggers the "Who are you?" alert is because the isAdmin and isStudent are set to false. An if loop runs the code blue when the condition evaluates to true.