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 trialLuqman Shah
3,016 PointsI know how to do it, but I do not understand it..
So this code challenge pretty much goes over the whole boolean values concept. I didn't have much trouble going through these past 3 code challenges, I knew what I was doing, I just didn't know why. Why do our isAdmin and isStudent variables need boolean values? And why are they both set to "false." So during the if statement when we put the isAdmin variable into the condition, obviously we are trying to say "If isAdmin is TRUE, run code: alert('Welcome admin');" And the else if clause is an alternative, to my understanding, so we are saying "If you are not an admin but isStudent, and if it is true, run code: alert('Welcome student');"
But what is the point of these boolean values? How does it benefit us? And why is it initially set to the false value. I watched the video on boolean values twice, I even watched a video on boolean values on youtube but I still don't get it. Wouldn't this also be sufficient: Why don't we write the code like this?:
var isAdmin
var password = prompt('admin password');
var isStudent
var password_two = prompt('student ID');
if ( parseInt(password) === isAdmin ) {
alert('Welcome Admin');
} else if ( parseInt(password_two) === isStudent ) {
alert('Welcome Student');
} else {
alert('Who are you?');
}
var isAdmin = false;
var isStudent = false;
if ( isAdmin ) {
alert('Welcome administrator');
} else if (isStudent) {
alert('Welcome student');
} else {
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>
Dave StSomeWhere
19,870 PointsAgree with Steven's answer you might be over thinking the lesson, which is just understanding how conditional statements work. There are two main parts to a conditional - the expressions (which evaluates to either true or false) and the code block (of course, you have else and else if). Boolean's by definition are either true or false and using them in the expression makes for clean readable code.
// Just a boolean
if (isAdmin) {// code block here}
// is a little prettier than
if (isAdmin === someValue) {// code block here}
1 Answer
Steven Parker
231,268 PointsI think you're reading too much into the code challenge. It's just to give you practice working with program flow and boolean variables. It's not useful as a complete program in itself.
Your other code is a more practical example, but as you point out, it doesn't serve as an example of boolean variable use.
A practical example of both might be where many tests were made to determine administrator status, only if a certain combination passed would "isAdmin" be set to true. Then later in the program "isAdmin" would be used as part of input validation and output controls.
Luqman Shah
3,016 PointsAhh ok, thank you. That really clears it up for me
Luqman Shah
3,016 PointsLuqman Shah
3,016 PointsI looked over my notes again, I feel like I'm getting it, but would still love to get your help, thank you.