13.) Not "My code works and I don't know why...."


#1

Okay yeah, that's a little programmer's joke from a meme, anyway, lol...I'm trying to explain to myself the logic of this code as well as the requirement from the exercise.

// Declare your variables here!
programming = false;

var happy = function() {
  // Add your if/else statement here!
  if(!programming){
      return true;
      }
      else {
        return false;  
        }
};

the console shows false, and the lesson is completed.

As I quote from the lesson:

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.

Since it was globally declared that programming variable is false at the very start, when the variable is subjected to the Not Operator in the If Statement, then the If statement should be true (1), and the function should then return True. Is it not?

So why is it that the answer at the console is shown to be False? Doesn't that mean that the If Statement is false? I feel like I'm looping on the logic here...it just doesn't make sense to me...


#2

yes!!! you are right! :slight_smile:

because you didnt call the function.you have to call the function.The code in a function is not executed when the function is defined. It is executed when the function is invoked.Some people use the term "call a function" instead of "invoke a function".
now do

programming = false;

var happy = function() {
  // Add your if/else statement here!
  if(!programming){
      return true;
      }
      else {
        return false;  
        }
};

happy();

i hope now you'll understand it :slight_smile:


#3

just to make what rcodemansaid more clearer, the "false" came from your first line code, programming=false.


#4

I see! Marblexake's answer explained where the false came from which made me confused in the first place, and rcodeman, you rock! I better call the functions for something like this in the future so I won't be so confused anymore looking at the console.

Thanks a lot to you both!


#5

You're welcome :smiley:


#6

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