Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Add an Else If Clause

I have written the code correctly but its still saying there is an error

else if(isStudent){ alert("Welcome student"); }

thats what I was asked to do and i did it but still its not accepting

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

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

3 Answers

andren
andren
28,520 Points

Your code is indeed fine. The issue is that the code checker for these challenges is often extremely picky about how your strings are written. They usually have to match the string that is requested exactly as the instruction specifies.

The challenge asks you to print Welcome student you print Welcome Student (The S in student is capitalized). If you fix that inconsistency then your code will pass the challenge.

Stefan Jesner
Stefan Jesner
3,353 Points

wrong, he forgot a semicolon

Loek Weijts
Loek Weijts
6,159 Points

But there is an error because in:

if ( isAdmin ) {

there is nothing defined.

Stefan Jesner
Stefan Jesner
3,353 Points

No if ( isAdmin ) {

means that the isAdmin bool = true

Loek Weijts
Loek Weijts
6,159 Points

But i dont get it:

var isAdmin = false;

so how can if ( isAdmin ) be true when its just defined as false?

So logical if you want to get true as an answer, you should compare them:

if isAdmin === true;

then print the message he's an administrator, but it's not.

Ryan S
Ryan S
27,275 Points

Loek,

The if( isAdmin ) statement doesn't declare whether the variable is true or false, it checks whether it is true or false. It is a shorter way of writing if( isAdmin == true). Keep in mind this is a conditional statement, not a variable definition.

In this case, the code is basically saying "if isAdmin is true, then send an alert." But since the actual value of isAdmin is false, the if ( isAdmin) statement is not true (evaluates to false) and the alert statement will not run.

Hope this clears things up.

andren
andren
28,520 Points

Stefan Jesner: Semicolons in JavaScript are not mandatory. They are only required if you are writing multiple statements on the same line. And the line that is missing a semicolon is code that exists in the challenge when you first load it. In other words it was code written by Treehouse, not by Kumar.

That's right! Thanx

Stefan Jesner
Stefan Jesner
3,353 Points

You forgot to put a semicolon

var isAdmin = false;
var isStudent = true;

if ( isAdmin ) {
    alert('Welcome administrator');
}
else if(isStudent==true){
  alert('Welcome Student');
}
Loek Weijts
Loek Weijts
6,159 Points
var isAdmin = false;
var isStudent = true;

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

Is the code, thats correct in workspaces.

Ryan S
Ryan S
27,275 Points

Andren is correct. It is a matter of the capital 'S' in "Student". It should be lowercse.

Semi-colons in JS aren't technically necessary if the statement is followed by a line break, especially if there is only one statement in a block, however it is best practice to include them.

Also, it should be noted that the statement without a semi colon was actually provided by the code challenge. The only code you are required to write was the "else if" statement. It will pass without the semi-colon.

Its not me! The code was written like this only and i have just added the: if(isStudent){ alert("Welcome Student");} only and semicolons are not mandatory in JS

Loek Weijts
Loek Weijts
6,159 Points

I use atom as my editor, thats why i use window.alert, but also without is fine:

var isAdmin = false
var isStudent = true

if (isAdmin === true) {
  window.alert('Welcome administrator')
} else if (isStudent === true) {
  window.alert('Welcome Student')
}
Stefan Jesner
Stefan Jesner
3,353 Points

where are the semicolon?