Why is this?


#1

https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/2/4?curriculum_id=506324b3a7dffd00020bf661

I'm not understanding this logic...

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

var programming = false;
  • Then, if programming equals false, your happy function 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, 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.

Is moderator a paid position?
#2

HI the instruction says..
Then, write an if/else statement inside happy so that happy returns true if programming is false and false otherwise.
so if you do that

if (!programming) {
   //this

since programing is false with the ! it will become true right?

!true;   // => false
!false;  // => true

so the if condition will be true and will return return true


#3

Exactly. You would think this is incorrect because the instructions ask to return true when programming is false, but it will only pass you when programming is true and returns true.


#5

OK, so this is a little confusing at first :slight_smile:

The code isn't taking into account programming's current value. It only knows whether programming is true or false. So, if (programming) will check if programming is true. if (!programming) will check if programming is false, regardless of the original value.


I was trying to figure out what was going on earlier before arriving at that simple conclusion ↑, and I found out that if by running this code:

var programming = false;

var happy = function() {
    console.log("1: " + programming); // original value
    console.log("2: " + !programming); // opposite value
    console.log("DONE"); // marker to keep my place
    
    if(!programming) {
        return true;
    }
    else {
        return false;
    }
};

we get this output:

1: true
2: false
DONE
1: false
2: true
DONE

Something odd is going on there. We set programming to false originally, but now it's somehow magically true all of a sudden. A quick look around revealed that the SCT is changing it:

// ...
try {
 var pros = [true, false]; // 
 for (var i = 0; i < pros.length; i++) {
  programming = pros[i]; // the value we set `programming` to
                         // doesn't matter because the sct is 
                         // messing with it - see? 
  if (ans(programming) !== happy()) {
   return "It looks like your happy function returns " + happy() + " instead of " + ans(programming) + " when programming is " + pros[i];
  }
 }
} catch (e) {
 return "Your code caused the following error: " + e;
}
return true;

So the SCT calls happy() twice, one with programming true, and one with itfalse.


Something that's extremely helpful for this type of thing is to boil down the logic to it's simplest, and to use console.log() for debugging stuff like I did. By simplifying the instructions so I didn't have to think about what they meant every time, I could look at what was going wrong easier:

// ... 
if (programming) {
  return true;
// ...
// instructions
// programming false, happy true
// programming true, happy false
// error
// programming true, happy false
// but happy true

// error:
// > It looks like your happy function returns true 
//   instead of false when programming is true

By doing that, I could compare the expected and actual results much easier, and see that it wasn't just the SCT thinking strangely, it actually did need !programming to get the correct answer.

Then I was able to remember how if statements work, and hopefully answer your question satisfactorily :slight_smile:


#6

thanks so much! I really appreciate it!! :slight_smile:

I think I understand that whenever you have:

if (conditional) {

It will automatically read if (true) {

no matter the value of said conditional..

Again thank you so much!