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:
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./t/why-is-this/42458
Share Improve this question edited May 22, 2016 at 21:01 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked May 19, 2016 at 17:11 NickNick 471 gold badge2 silver badges12 bronze badges 11- 2 Possible duplicate of What is the !! (not not) operator in JavaScript? – t0mm13b Commented May 19, 2016 at 17:14
-
Consider this: what is
not false
? What isnot true
? Ifprogramming === false
then!programming
isn't going to producefalse
. – Mike Cluck Commented May 19, 2016 at 17:15 -
1
var happy = function() {return !programming;};
is much more concise. – Niet the Dark Absol Commented May 19, 2016 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 whenprogramming
istrue
. Perhaps you're just running the wrong test? – T.J. Crowder Commented May 19, 2016 at 17:16 -
If
programming = false
then what makes you think that!programming
evaluate to false? – IMTheNachoMan Commented May 19, 2016 at 17:18
8 Answers
Reset to default 8I'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
};
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 parison operator ==
or !=
, or the negation operator !
your exercise hints at.
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 ments 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
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
}
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
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;
}
};
'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.
...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.