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 trialKUMAR ANKIT
700 PointsI 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
var isAdmin = false;
var isStudent = true;
if ( isAdmin ) {
alert('Welcome administrator')
}
else if(isStudent==true){
alert('Welcome Student');
}
<!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
28,558 PointsYour 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
3,353 PointsYou 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
6,159 Pointsvar 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
27,276 PointsAndren 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.
KUMAR ANKIT
700 PointsIts 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
6,159 PointsI 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
3,353 Pointswhere are the semicolon?
Stefan Jesner
3,353 PointsStefan Jesner
3,353 Pointswrong, he forgot a semicolon
Loek Weijts
6,159 PointsLoek Weijts
6,159 PointsBut there is an error because in:
if ( isAdmin ) {
there is nothing defined.
Stefan Jesner
3,353 PointsStefan Jesner
3,353 PointsNo if ( isAdmin ) {
means that the isAdmin bool = true
Loek Weijts
6,159 PointsLoek Weijts
6,159 PointsBut 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
27,276 PointsRyan S
27,276 PointsLoek,
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 writingif( isAdmin == true)
. Keep in mind this is a conditional statement, not a variable definition.In this case, the code is basically saying "if
isAdmin
istrue
, then send an alert." But since the actual value ofisAdmin
isfalse
, theif ( isAdmin)
statement is not true (evaluates to false) and the alert statement will not run.Hope this clears things up.
andren
28,558 Pointsandren
28,558 PointsStefan 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.
KUMAR ANKIT
700 PointsKUMAR ANKIT
700 PointsThat's right! Thanx