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 an Else If Clause

Mihia Maftei
Mihia Maftei
1,315 Points

Hi i don't really see what is wrong for me is looking good the code right now can you explain me what i done wrong thx

Do i need to put an 'else' to the end?

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

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

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

You should not change the condition in the if-statement.

var isAdmin = false;
var isStudent = true;

if ( isAdmin ) { // dont change this
    alert('Welcome administrator');
} else if ( isStudent ) { // here create the 'else if' to check if 'isStudent' is true
    alert('Welcome student'); // then create the alert
}
Mihia Maftei
Mihia Maftei
1,315 Points

Thx for the quick help after when i send the msg i found what i done wrong thank you again !

Ari Misha
Ari Misha
19,323 Points

Hiya there! There are few bugs in your code. First, you changed the code that came as a default to the challenge. Dont do that unless challenge explicity ask you to. Second, "else if" block takes a boolean expression to evaluate, which you didnt provide any, and when it returns true, it executes the code inside the code block. Here is how your code needs to look like:

var isAdmin = false;
var isStudent = true;

if ( isAdmin ) {
    alert('Welcome administrator');
} else if (isStudent) {
    alert('Welcome Student');
}
Mihia Maftei
Mihia Maftei
1,315 Points

Thx for help i found what i done wrong after 10 sec when i send the msg but thx so much for support

You aren't checking anything in your else if clause. Else if conditionals need a clause to check for the code after it to fire. It appears you've maybe confused else and else if.

Owa Aquino
Owa Aquino
19,277 Points

Hi Mihia,

You can remove the "if ()" on your condtion if you don't have any additional condition. We only use them if we intended to have at least multiple condition..

see here:

if ( isStudent = true) {
    alert('Welcome administrator');
}else {
    alert('Welcome student');
}

Here are example of using if.. else if.. else if. else on a scenario where an actor audition. (just for fun)

if (actor = 'dance') {
 alert('user can dance! that will do');
} else if( actor = 'sing') {
 alert('actor can't dance but he surely can sing. that will do');
} else if(actor = 'act') {
 alert('actor can't dance, can't sing but he sure can act. that will do');
} else {
 alert('sorry actor didn't pass the audition');
}

Hope this help!

Owa