Functions, return and if/else


#1


I don't understand what Introduction to Functions in JS, exercise 8 is asking me to do. What does "This function returns a value equal to one quarter of the parameter." mean?

https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/1/3?curriculum_id=506324b3a7dffd00020bf661

The error message says: "SyntaxError: Illegal return statement"

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

quarter(12); 

I'm trying to print "The statement is true"

#2

This function is missing. Plus, we cannot write return inside an if statement unless it is inside a function. In this case, that line will be removed from where you have it.

Create the funciton...

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

// your if statement sans the return line

Now your if statement will work as expected if you write this,

quarter(12)

instead of

quarter(number)

#3

Is that your whole code? Where's your quarter function?
You need to define quarter as your function and it should have a parameter number, and your function which is defined as quarter should return a value equal to one quarter of the parameter.

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

After that, inside your if statement's condition, you need to put a number as your parameter value. Not literal word number, but give a value, for example (12). Like this:

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

Hope this helps.


#4

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