-1

I am working on a JavaScript exercise on an interactive website, and I just really need some assistance in understanding the logic behind this...

The exercise asks that you define a variable programming, and set it equal to false.

var programming = false;

Then, if programming equals false, a function, happy is to return true.

programming has already been set to false, so my first thought was to just write:

if (programming) {
    // this

I made the mistake of not using the ! operator, which is what they were requesting, so I then tried to write:

if (!!programming) {
    // this

To me, this says: not not false which I thought would cancel out and equal false

But I get the following error:

Oops, try again. It looks like your happy function returns true instead of false when programming is true

this works:

if (!programming) {
    // this

I'm just not understanding why (!programming) evaluates to false, when I believe this is basically saying: (!false)

Please help me understand the error of my ways. Thank you.


For a reference, here is my full code:

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

Edit:

I've found the solution. zystvan explains it on this post: https://discuss.codecademy.com/t/why-is-this/42458

halfer
  • 19,824
  • 17
  • 99
  • 186
Nick
  • 47
  • 1
  • 2
  • 12
  • 2
    Possible duplicate of [What is the !! (not not) operator in JavaScript?](http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript) – t0mm13b May 19 '16 at 17:14
  • Consider this: what is `not false`? What is `not true`? If `programming === false` then `!programming` isn't going to produce `false`. – Mike Cluck May 19 '16 at 17:15
  • 1
    `var happy = function() {return !programming;};` is much more concise. – Niet the Dark Absol May 19 '16 at 17:16
  • *"Oops, try again. It looks like your happy function returns true instead of false when programming is true"* You've started out with `programming = false`, but then this error is saying your code is wrong when `programming` is **`true`**. Perhaps you're just running the wrong test? – T.J. Crowder May 19 '16 at 17:16
  • If `programming = false` then what makes you think that `!programming` evaluate to false? – IMTheNachoMan May 19 '16 at 17:18
  • when something is not false it is true. so when you saying !false it gives true back the not operator before a boolean gives the opposite of a boolean. when you say twice not like the !! does, you say twice opposite, what will returns the same as the original..... get it ? – Ralf D'hooge May 19 '16 at 17:20
  • `var happy = !programming;` is even more concise – mplungjan May 19 '16 at 17:26
  • ^Not a function return though. `const happy = () => !programming` – mferly May 19 '16 at 17:27
  • @IMTheNachoMan I understand that completely. I know that !programming evaluates to true, which is exactly why I was confused. The exercise wanted you to return true when `programming = false`, which is why I assumed passing `!!programming` would return false, but the error said I was returning true instead of false when `programming` is true.. – Nick May 19 '16 at 17:36
  • I am not understanding where the confusion is coming from. `programming = false`. So: `!programming` => `!false` => `true` and `!!programming` => `!!false` => `!true` => `false`. – IMTheNachoMan May 19 '16 at 17:38
  • @IMTheNachoMan I've no idea either, but at least that's a good exercise in pedagogy :) – Aaron May 19 '16 at 17:42

8 Answers8

8

I'm just not understanding why (!programming) evaluates to false, when I believe this is basically saying: (!false)

This syntax:

if (!programming) {
}

Is essentially shortform for:

if (programming != true) {
}

Which is another way of writing:

if (programming == false) {
}

I think not understanding this is the source of your confusion.


Additionally take note of the following:

var programming = true;
programming;   // true
!programming;  // false
!!programming; // true

programming = false;
programming;   // false
!programming;  // true
!!programming; // false

So your program could be shortened to:

var programming = false;
var happy = function() {
    return !programming; // returns true when programming is false
};
David Sherret
  • 101,669
  • 28
  • 188
  • 178
  • 1
    Wondering that myself, its an important concept for someone beginning programming – element11 May 19 '16 at 17:17
  • I get that normally when you might say, for example, `if (this) {` would basically mean, `if (true) {` but where my confusion is coming from is that you already assign the `this` variable to false and then pass it as an argument, so if you were to now say `if (this) {`, would it not be reasonable to perceive that as `if (false) {` ? – Nick May 19 '16 at 17:20
  • `if (this)` would only evaluate to true if `this` was _truthy_, e.g. not null, not empty, not 0 and not undefined which are all _falsy_ values – mplungjan May 19 '16 at 17:21
  • @Nick don't think of it as `if true`... think of it as `if this is truthy` – David Sherret May 19 '16 at 17:24
1

if (programming) does not mean "if programming is set to false", it's actually the opposite ! The syntax of a simple if block is the following :

if (condition) { action }

And it is executed in the following way :

if condition evaluates to true, then execute the action

The exercise asks you to execute the action only if programming is false. "programming is false" is your condition here. You know it's currently true, so you could as well write if (true) { action } or simply action, but your exercise would probably consider this cheating (usually the condition won't be so obvious).

So what you need is to produce a condition that will evaluate to true if and only if programming is false. You could use a comparison operator == or !=, or the negation operator ! your exercise hints at.

Aaron
  • 24,009
  • 2
  • 33
  • 57
1

EDIT: The image you have just posted contains different code from the code pasted into your question. Your question used !programming but your image shows !!programming. Change it to !programming (as per your question) and it should be fine.

I think your program is correct - are you sure you executed it correctly and are you sure that the test is correct?

Pasting your code into node gives the following ...

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

... so when programming is true your code returns false which is the desired result and contrary to the test result?

Oops, try again. It looks like your happy function returns true instead of false when programming is true

You could shorten your function BTW to:

var  happy = function() { return !programming; }

And finally beware the comments above that

!programming

is equivalent to

programming != true

This is not necessarily true if programming has a non-boolean value!! Consider for example the following:

> programming = 5;
5
> !programming
false
> programming != true;
true

or better still

> var sad = function(value) { return (!value) === (value != true) }
> sad(undefined)
true
> sad(null)
true
> sad(false)
true
> sad(true)
true
> sad(1)
true
> sad(2)
false
> sad("")
true
> sad("A")
false
haggisandchips
  • 523
  • 2
  • 11
0

When you use:

if (!someVariable) {

}

it is saying "if the value of someVariable is not truthy (any value other than null, undefined, or false"), then execute the body of this "if".

So, when you do this:

var someVariable = false;

if (!someVariable) {
    //Since someVariable is not truthy, this will run
}
mcgraphix
  • 2,723
  • 1
  • 11
  • 15
0
var programming = false;
var happy = function() {
  if (!programming) {
      return true;
  } else {
      return false;
  };
};

is the same as

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

So you have two cases

  • programming = true
    • if (true != true) => is not true => else block is called => false is returned
  • programming = false
    • if (false != true) => is true => if block is called => true is returned
Andi Giga
  • 3,744
  • 9
  • 38
  • 68
0

I think you may have more semicolons that cause conflict in execution time, so if the function doesnt execute right the variable is set to null and you get a False in return.

Try to just remove the second semicolon, like:

var programming = false;
var happy = function() {
  if (!programming) {
      return true;
  } else {
      return false;
  }
};
Adan Sandoval
  • 436
  • 1
  • 6
  • 18
0

'true' or 'false' is sometimes confusing even for an experienced programmer.

Instead of counting 'true' or 'false', treat them as 'success' and 'fail'. An expression can be 'success' or 'fail'.

if(expression){
  I'll be only executed when 'expression' is a success exp.
}

So when you write

var programming = false;
if(programming){}

programming is a fail expression, so anything in the 'if' won'be executed.

sfy
  • 2,810
  • 1
  • 20
  • 22
0

...if statements execute the following block of code if the statement in the parens evaluates as true/truthy.

Everything you write is, for some reason, on the assumption that since your var is false it should pass the if statement. I have no idea where you'd get that idea from.

So yeah, if (programming) where programming is false will not execute the code in the if statement that follows.

And likewise, you're right that !! will just flip it twice, back to a boolean of whatever it originally was (truthy to true or falsey to false)...and it was originally false...so yeah, again, the if statement would not pass and not execute the code.

! flips the false value to true, which makes it pass the if statement. So yeah, if you want a variable to pass an if statement if the value is false, then if (!programming) is exactly what you'd do. It's not evaluating to false...it's evaluating to true, which is why it then passes the if statement.

Jimbo Jonny
  • 3,549
  • 1
  • 19
  • 23