13. Not -- I feel like the solution is wrong


#1

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

The exercise instructions are:

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.

Below is the code I initially wrote, but it said it was wrong until I swapped "return false" with "return true":

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

I don't understand how my code is wrong. The instructions say that happy should return "true if programming is false and false otherwise." As per the instructions, programming is initially declared as false, which means !programming yields true, which would return false. Therefore, else would return true if programming is false. Did the instructions mean for us to set programming to true at first?

Thank you guys for your time! I would greatly appreciate any direction to help me understand the issue.


#2

// Declare your variables here!
var programming = false
The only reason your code didn't work the first time is cause you declared the variable programming as a global variable which will still work but in doing so you confused yourself,

So if you do not declare any variable before your function your previous code will work,

// No variable before or after happy function

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

#3

Thanks for the response! You mention that if I did not declare a global variable at first, the code would run correctly.
However, I am trying to understand if I followed the codeacademy instructions properly. If I did not, I was hoping someone could explain where I went wrong, instead of giving me an alternate solution to the instructions. Thanks again!


#4

I did not give you an alternate solution actually you said that at first this is what you tried,

// No variable before or after happy function

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

You went wrong when you declared as a variable outside of your function. :slight_smile:


#5

Hmm, but the instructions explicitly state to "Declare a variable called programming and set it to false." Therefore, I declared the variable.. :confused:

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

#6

Oh wait sorry totally my fault yes you did the right thing its just that your code at first,

Was slightly incorrect because if programming was declared false that means that inside your function writing !programming would be the same as writing !false which in turn is supposed to return true and notreturn false. Sorry for the confusion in the beginning. :slight_smile:


#7

The instructions state, "write an if/else statement inside happy so that happy returns true if programming is false and false otherwise." In other words, if programming is true, the if/else statement should return false, correct?

In my if/else statement !progamming is the same as !false which is also the same as true. This means that my if/else statement would return false when programming is true. Isn't that correct?

Therefore, wouldn't it mean that the instructions on that page are incorrect if my code is considered to be wrong when I click submit?


#8

You are correct if the code that passes the SCT is not the same code the instructions gave you then that is a bug in the exercise which you can report on the exercise itself. :slight_smile:


#9

hi bandit, I also think instructions in this lesson contradict the answer. Where is the button that i can press to report the bug? :slight_smile: . I don't see it in the exercise itself.


#10

It is under the lesson instructions bottom-left you will see small text that says Report a Bug?


#11

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

@stanleywong, did you call function happy???


#12

This was my code that worked:

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

};


#13

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