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

Eric C
Eric C
7,998 Points

Help with JavaScript challenge

I can't seem to get this to pass, what am I missing? Thanks

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

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

2 Answers

Douglas Counts
Douglas Counts
10,060 Points

Hey Eric,

You have a couple of problems here. Firstly you are doing assignments to the variables instead of comparing their value to true. Use three equal signs isStudent === true to do a comparison. like so:

} else if ( isStudent === true ) {

Your else-if statement was out of order too. Notice about that the comparison comes after the if keyword. Think of it as an else-if statement, that way you don't get the order of those two keywords wrong.

Secondly, instead of comparing your values to true, you can simply check the boolean value directly like so:

if ( isAdmin ) {

which is easier to read and involves one less logic step than

if (isAdmin === true) {

Good luck....

Eric C
Eric C
7,998 Points

thank you

Neil Docherty
Neil Docherty
10,418 Points

Couple of things to note.

  1. You need to use 'else if' and not 'if else'

  2. The variables "isAdmin" and "isStudent" already have boolean values assigned to them. There is no need to check if they are true using (isStudent = true).

  3. The code challenge is picky and doesn't like the capitalisation of 'Student' in your alert message. (This is not a coding error)

Here is the working solution:

var isAdmin = false;
var isStudent = true;

if ( isAdmin ) {
    alert('Welcome administrator')
} else if ( isStudent ) {
    alert('Welcome student')
}
Douglas Counts
Douglas Counts
10,060 Points

LOL, you got your answer submitted before mine. Noticed though that you didn't mention that using a single equal sign is actually making an assignment to the variable instead of doing a comparision. Always use 3x equal signs to do a comparison. Making assignments instead of comparisons is one of the top JavaScript errors.

Neil Docherty
Neil Docherty
10,418 Points

No need to use the identity operator.

The variables of isAdmin and isStudent are assigned a boolean ( true / false ).

The if statement checks if the condition is true. isAdmin returns false whereas isStudent returns true.

It would be different if the code was set up like this:

var isAdmin = "false";
var isStudent = "true";

if ( isAdmin ) {
    alert('Welcome administrator')
} else if ( isStudent ) {
    alert('Welcome student')
}

In this situation the variables of isAdmin and isStudent are assigned a string of ( "true" / "false" ). This means that the conditional statement if ( isAdmin ) returns true because isAdmin has a 'real' value (i.e. it's not empty and it's not 0). The alert('Welcome administrator') will execute in this case.

This is where you're quite right in saying that we should use the === identity operator.

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