13/15 Best Practice Tips?


#1

Hi,

I wrote this script and got a 'pass' after submitting:

var programming = false;

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

};

For line 4:
// should I be writing it all out like this
if(programming == !fase)
// or do i need to write like this
if(!programming == true)
// or can I just do it like the way I did it in my example?

I realize all of the above methods give me the same result, but what is the best method to stick to in practice?

Thanks for your help!


#2

Well you have 2 possible inputs true and false and you can think of the outputs:

Version 1:

!programming

input: true -> not true -> output: false
input: false -> not false -> output: true

Version 2:

programming == !false

input: true -> true is equal to not false -> true is equal to true output: true
input: false -> false is equal to not false -> false is equal to true output: false

So this is not equal to Version 1.

Version 3:

!programming == true

input: true -> not true is equal to true -> false is equal to true output: false
input: false -> not false is equal to true -> true is equal to true output: true

this is equal to Version 1.

As a rule of thumb, if you know what you're doing and you made sure the the input is only true and false, than the shortest one wins !programming. But if the input is generated by a user and you did not make sure that the input is only a string, then it might be better to use === boolean instead.


#3

Thanks, that is really helpful!