Question 8-intro to functions/if else


Can soemone please tell me whats wrong with my code?

var quarter = function(number) {
   return number/4;


if (quarter(9 % 3) === true ) {
  console.log("The statement is true");
} else {
  console.log("The statement is false");


Firstly, you are missing a semicolon (;) at the end of your function definition. Add that.

Also, in your conditional, you are asking the interpreter to evaluate whether or not the remainder of 9/3 (0) divided by 4 (also 0) is true, which it is not.

0 = false
1 = true

What are the exercise instructions?


Define a function called quarter which has a parameter called number.
This function returns a value equal to one quarter of the parameter. (i.e. number / 4;)
Call the function inside the if statement's condition (and put in a parameter value!) such that "The statement is true" is printed to the console.


OK. quarter(9 % 3) returns false because 0 maps to false whereas 1 maps to true. Make it return 1, and it will be processed as true.

Make sense?


the instructions say We want quarter(some number) that when divided by 3 has remainder 0.


Then re-work the conditional such that you are not checking whether the value returned by quarter(number) is true. Check, for example, whether it returns a value that is false...this would make the condition true :wink:

Confusing enough?


confusing would be an understatement! im going insane lol



if (quarter(9 % 3) === false) {
 // true!
} else {
 // false!

Again, 0 maps to false whereas 1 maps to true. quarter(9%3) returns 0, therefore false so by checking if the result returned is false (0), you are getting a conditional evaluation of true. :slight_smile:


OMG I just saw the error!! the code should make it === 0 and not "false/true"!!!


true === 1 and false === 0, so it's the same thing. But yes, you are right!


thanks for your speedy replies! stay awesome!


true === 1 returns false because it are different types:

typeof true -> "boolean"
typeof 1 -> "number"

but true == 1 returns true.


Ah, you are correct. My apologies. I must've forgotten about JavaScript's different equality checks.

I'm more of a Java guy, myself. :slight_smile: Nice work.


I saw how you recreate the JavaScript course’s “rock, paper, scissors” exercise in Java and it looks great!


Not in JavaScript. It converts to 1 or 0, depending, but is not an identity.

 > true === 1
=> false
 > false === 0
=> false
 > true == 1    // conversion
=> true
 > false == 0   // conversion
=> true


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