Understanding 13: Not


#1

INSTRUCTIONS:

Declare a variable called programming and set it to false. Then, write an if/else statement inside happy so that happy returns true if programming is false and false otherwise.

Here is my code:

var programming=false;

var happy = function() {
  if (programming) {
      return true;
  }
  else {
      return false;
  }
}

This gives me:

Oops, try again. Make sure to use the 'not' operator (!)

But why would I need ! here? It tells me to open with programming set to false. Then it tells me to return true if programming is false. So it's already false! It seems to be giving me contradictory instructions.

I tried this:

var programming=true;

var happy = function() {
  if (!programming) {
      return true;
  }
  else {
      return false;
  }
}

And I got this:

Oops, try again. Did you set programming to false?

What am I not getting?


#2

! (NOT, or negation operator) is a boolean operator that will toggle true to false or false to true. We set programming to false so that when it is negated in the condition the program will return its boolean opposite. That's what negation is.


#3

Yes, I know that.

It wants me to set programming to false and then return true if programming is false. Well, programming is false. !programming is true, which should return false.


#4

If programming is set to false, !programming will be true. It doesn't change the value of programming, only the evaluation outcome.

We're not testing if programming is false. We are only checking the negated value. In other words, unlike what many have done in the past, we would not write this...

if (programming === false)

Simply,

if (!programming)

programming is still false as set. The outcome will be true when that is negated.


#5

Would it be better to say

if (programming != true)

or is that the equivalent but less efficient version of

if (!programming)

?


#6

When testing a boolean, we never need to actually write it in the conditional.

if (programming)

is enough. JavaScript can see the boolean primitive hidden in the variable so no literal representation is ever needed.


#7

Yes, but it wants us to initially set it to false and use the negation operator. Since programming then evaluates to false, that would be the trigger for the happy function to return true. !programming evaluates to true, which would be the trigger for the happy function to return false.

This is what's making me think it's giving us contradictory instructions, and this confuses me because it accepts

var programming=false;

var happy = function() {
  if (!programming) {
      return true;
  }
  else {
      return false;
  }
}

#8

It accepts this because it is correct. programming IS false. No evaluation needed. !programming is the boolean opposite, true.


#9

So it's the wording of the instructions that's confusing me. Because in the past when they've given similar instructions, what they wanted was pretty straightforward. This one suddenly wanted something a little different, and that really threw me.

Thanks.


#10

So here is the code that worked for me.

var programming = false;

var happy = function() {
// Add your if/else statement here!
if (programming===false) { // <-- not sure why this was necessary since we already declared it false
return !false;
} else {
return false;
}

};


#11

In my line of reasoning, we're comparing the declared value of programming against an explicit boolean statement. So yes, earlier we assigned var programming = false. Now if we say if(programming), that's the same as saying if(false). But if we say if(programming===false) then that would evaluate to true. Per the instructions, we want happy to return true if programming is false and return false if programming is true.


#12

Thank you. for putting in words what I was thinking but could not express.

So what you are saying is that for the sake of explicitly evaluating the statement in the if clause, we need to make sure that the variable "programing" is equal to false to return it as true?


#13

Wait, I thought you were helping to explain things to me, because as you can see from my first post, I'm a little confused.

Don't hijack other people's posts for questions. Make your own.


#14

My apologies.

Will do.


#15

So we understand... When a variable represents either true or false explicitly, no comparison is necessary. Just,

if (programming)

is enough. We would never write,

if (programming === false)

as it is pointless to do so, and redundant. We do not want to know if programming === false. We want to demonstrate the act of negating a boolean value.

!programming => ! false -> true

if (!programming) {
    return true;
} else {
    return false;
}

What's more, we don't need an if statement, at all.

return !programming;

Bear in mind that programming is not changed from false, but the negated value is examined. Study this concept in full. As dull and boringly simple as it seems, it is a fundamental concept in boolean logic. Don't disregard it or consider it unnecessary.


#16

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.