Question 8-intro to functions/if else


#1


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");
}


#2

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?


#3

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.


#4

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?


#5

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


#6

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?


#7

confusing would be an understatement! im going insane lol


#8

e.g:

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:


#9

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


#10

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


#11

thanks for your speedy replies! stay awesome!


#12

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

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

but true == 1 returns true.


#13

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.


#14

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


#16

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

#17

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