8. Functions in Javascript


#1

Hi, wondering if you could me out here with my code, I keep getting false.

` // Define quarter here.
var quarter = function(number) {
return number/4;
};

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

Thanks in advance!


#2

Hi you Getting false because

100 % 3 = 1

Try ti change the 100 by 9 so it will be

9 % 3 = 0

#3

Got it, I used 60, but i dont kow how to close the thread hahaha


#4

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

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

Any suggestions why it doesn't work?
Thanks in advance : )


#5

Because

is a function call, you are passing 9 to this function

and get returned 9/4

so you are trying to see if (9/4 % 3 === 0){} and it doesn't.


#6

Thanks a lot!! It worked now.


#7

After reading the message boards, I am still struggling with exercise 8: Functions in Javascript. Here is my code that I have so far: // Define quarter here.
var quarter = function (number)
{return number / 4;};

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

Syntax Error: expected expression, got keyword 'else'

Any help would be greatly appreciated. Thank you.


#9

No semicolons after conditions:

if (quarter(12) % 3 === 0);{


#10

if (quarter("") % 3 === 0);{

works for all possibilities, it wasn't taught in there yet though, I just assumed from other languages.
the double quotations pretty much mean open ended statement, so it is waiting for you to call quarter on its own later with any number.

var quarter = function(number){
    return number/4
};// Define quarter here.

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

its a neat little trick.


#11

the double quotations pretty much mean open ended statement, so it is
waiting for you to call quarter on its own later with any number.

No it just seems like "" has a numerical value of 0. Still, interesting as I'd expected that string/number is NaN. Also the semicolon after it will not work as mentioned in the previous post and you also fixed it in your code. So be careful what you copy :wink:


#12

The empty quotes set it to an empty string, that's global, so once I call in the function (number) it's now stored within the empty quotes.


#13

No parameters are by default local variable so you run the function once with "" as argument and once with 12 and the two calls are not depending on each other in any way. You're just lucky that the empty string has a value of 0 so 0/4 is still 0 and 0%3 is again 0 so it fulfils the condition.