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 trialBinu Alexander
4,081 Pointscant get this right .. what am i doing wrong ?
How do i make the boolean work ?
var correctguess = false
if (correctguess === true ) {
alert('This is true');
} else {
alert('This is false');
}
<!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
Gilbert Kennen
10,661 PointsYou have a small typo in the first line, there should be a semicolon at the end, but most interpreters will fix that sort of error.
This program will show the 'false' alert because in this configuration, the expression (correctguess === true) is false since (false === true) is false.
You have several ways you can get the true result. One way is to use the 'negate' operator '!' like this:
var correctguess = false;
if ( ! (correctguess === true) ) {
alert('This is true');
} else {
alert('This is false');
}
You could also change the value assigned to correctguess to be true:
var correctguess = true;
if ( correctguess === true ) {
alert('This is true');
} else {
alert('This is false');
}
Or you could change the value being compared against to be false:
var correctguess = false;
if ( correctguess === false ) {
alert('This is true');
} else {
alert('This is false');
}
Colin Bell
29,679 PointsIt's just looking for an explicit true
value.
if ( true ) {
alert('This is true');
} else {
alert('This is false');
}
Binu Alexander
4,081 PointsThank u Colin
Ryan Field
Courses Plus Student 21,242 PointsHi, Binu. While your answer will work, this challenge just wants you to put true
inside the conditional statement. The challenges can be somewhat picky when it comes to answers, though, so it's not entirely your fault! :)
Binu Alexander
4,081 PointsThank you Ryan
Colin Bell
29,679 PointsColin Bell
29,679 PointsVery solid explanation, but with regards to the challenge he's asking about none of these will pass it.
It just wants
true
inside the parentheses of theif
statement.Gilbert Kennen
10,661 PointsGilbert Kennen
10,661 PointsThis is true, but the error on that screen seems rather self-evident:
This felt more like someone trying to play around with the language based on what was just learned by combining additional concepts.