Function parameters

Tell us what’s happening:
Console.log(half(stats));

half is assigned a function which has no parameter. this function then returns a functions that has a parameter. How can this function be returned if the parent function does not have a parameter in the first place? It doesn’t make sense to me how the stats object can be passed when the parent function does not have a parameter for it.

Your code so far


const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};
const half = (function() {
  "use strict"; // do not change this line

  // change code below this line
  return function half(stats) {
    // use function argument destructuring
    return (stats.max + stats.min) / 2.0;
  };
  // change code above this line

})();
console.log(stats); // should be object
console.log(half(stats)); // should be 28.015

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.

Bear in mind that the code used is for the benefit of the tests, you only need to care about the bit in-between the “change code below this line/change code above this line”. Anyway…

Functions evaluate to whatever the return value is when you execute them.

So

function sayHello() {
  return "hello"
}

Calling that function means that it evaluates to the string “hello”. Therefore:

const hello1 = sayHello()
const hello2 = "hello"

Those two are the same thing.

You can return anything you like from a function, for example returning another function is totally fine:

function returnMeAdd () {
  return function add (a, b) {
    return a + b;
  }
}

Calling that function means it evaluates to another function. Therefore

const add1 = returnMeAdd();
const add2 = (a, b) => a + b;

Are the same thing, they are both functions. So then

add1(1, 2)
add2(1, 2)

Both do the same thing.

The code you posted doesn’t need anything passed into the parent function, because all the parent function does is return another function. It’s that function that takes arguments.

1 Like

Hi

// change code below this line
return function half(stats) {

The “half” and “stats” here are just placeholder names, ie., they could be any other valid name and the const half expression would still work. stats doesn’t refer to the stats object until it is passed as an argument in the console.log statement.

Hope that helps :slight_smile:

1 Like

Thank you so much. Can I ask another question related to the solution to this question here or do I need to make a new request?

If it’s not directly related, I’d open a new thread (it’s likely to be an issue that someone else has faced, so will be easier for people searching for help)