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 trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Booleans

Binu Alexander
Binu Alexander
4,081 Points

cant get this right .. what am i doing wrong ?

How do i make the boolean work ?

script.js
var correctguess = false

if (correctguess === true ) {
    alert('This is true');
} else {
    alert('This is false');
}
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

You 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
Colin Bell
29,679 Points

Very 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 the if statement.

This is true, but the error on that screen seems rather self-evident:

Bummer! Did you add true inside the parentheses of the conditional statement?

This felt more like someone trying to play around with the language based on what was just learned by combining additional concepts.

Colin Bell
Colin Bell
29,679 Points

It's just looking for an explicit true value.

if ( true ) {
    alert('This is true');
} else {
    alert('This is false');
}
Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

Hi, 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! :)