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

where is my mistake

I think my boolin is wrong

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

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

Hi neils,

else clause doesn't have any condition it just tells the program to execute a portion of code only if the condition of "if" returns false.

Right Solution would be :

var isAdmin = false;
var isStudent = false;

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

Here's how the above code works, there are three steps in it :

Step 1:

//Check if "isAdmin" is true or false, if it's true then run the code in the parentheses and if it's false then go to "else if"
if(isAdmin) {
alert('Welcome administrator');
}

Step 2:

//At this point isAdmin returned false so we will see the condition of "else if" , it checks isStudent and if it's true then run the code inside parentheses and if it's not then go to "else" part.
else if(isStudent) {
alert('Welcome student');
}

Step 3:

//At this point both isAdmin and isStudent were checked to be false , so simple execute the the code written in else parentheses.
else {
alert('Who are you?');
}

I hope it clears your concept, Happy Coding ! :)

  • Zain
Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Hi Niels Lostaunau,

  • You should remove the (false) condition after the final else. As this is an else, not an else if, there is no condition required.
  • I'd also bring that else up into the if statement :)

Hope that helps,

Dave.

Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

oh ... and because it's fussy about this kind of thing ...

  • change "who are you?" to "Who are you?" just to be sure :)

no offence but I still don't get it

Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

That's OK, I probably didn't explain it well enough.

I'll post the code here and hopefully it will make more sense :)

var isAdmin = false;
var isStudent = false;

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

Happy coding,

Dave

else clause doesn't use any conditional, that makes sense. I was missing that piece of information. thank you.