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 trialWes K.
Courses Plus Student 877 PointsWhat's wrong with my Challenge Task code?
See code below:
var isAdmin = false;
var isStudent = false;
if ( isAdmin = true ) {
alert('Welcome administrator');
} else if (isStudent = true ) {
alert('Welcome student');
} else if (isAdmin = false ) {
alert('Who are you?');
} else if (isStudent = false) {
alert('Who are you?');
}
<!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
Christopher Debove
Courses Plus Student 18,373 PointsHi there! In JavaScript the operator "=" is the affectation operator.
What you want to do in those conditions statements is to compare, so the operator "==" which is the comparison operator is what you need here.
By the way, looking a your code, take a look at this one :
if (isAdmin == true) { // IS ADMIN
// Welcome admin
} else if (isStudent == true) { // IS STUDENT
// Welcome student
} else { // IS NEITHER OF THEM
// Who are you?
}
Krishna Pratap Chouhan
15,203 PointsSinge "=" is assignment operator whereas for comparison we need "==".
Also you need to add one else clause at last, because that's what they expected and that is what the scripts must be looking in order to check your response.
var isAdmin = false;
var isStudent = false;
if ( isAdmin == true ) {
alert('Welcome administrator');
} else if (isStudent == true ) {
alert('Welcome student');
} else if (isAdmin == false ) {
alert('Who are you?');
} else if (isStudent == false) {
alert('Who are you?');
}else {
alert("Who are you?");
}
Christopher Debove
Courses Plus Student 18,373 PointsIn case of a "else" clause, there's no need for the two last "else if" clause. It's redundant.
Krishna Pratap Chouhan
15,203 PointsRight, but seems like @'Wes K.' is experimenting with the code and i believe its a good thing. Otherwise he wouldn't have edited the originally provided conditions in if clauses.
Wes K.
Courses Plus Student 877 PointsThanks everyone!