const half = (function() {
"use strict"; // do not change this line
// change code below this line
return function half({max,min}) {
// use function argument destructuring
return (max + min) / 2.0;
};
// change code above this line
})();
Could you explain a bit more this solution?
I am lost a bit.
Why we need to wrap the whole function creating an IIFE.
It seems a closure to me as well.
I have this solution but it is not accepted although I can understand this one.
const half = (function() {
"use strict"; // do not change this line
// change code below this line
return function half(stats) {
// use function argument destructuring
const{min, max} = stats;
return (max + min) / 2.0;
};
// change code above this line
})();
Same problem here.
I do not understand, why the code from @pradesh-kumar is a solution and the one from @Lacika1981 ist not?
And if pradesh-kumar’s solution is the correct one, how does the exercise description help finding the solution? This is really an issue with the new ES6 exercises (not sure if they are new, but before the update of the curriculum I hadn’t seen them). The examples in the exercises don’t help a lot in finding the solution.
In this challenge, there’s no particular reason (that I’m aware of) to use an IIFE. You can replace the entire IIFE with the code below, and FCC will accept the answer:
function half({min,max}){
return (min + max) /2
}
IIFEs are sometimes used as a way to apply strict mode to all the code contained within the IIFE, without requiring the entire external script to be in strict mode. However, since the IIFE in this example is returning a single function rather than a whole API, you could just as easily add ‘use strict’ into the half function declaration.
Your solution returns the correct answer, but in order to pass the FCC tests you need to
Use destructuring assignment within the argument to the function half to send only max and min inside the function.
Your solution destructures inside the function, rather than in its arguments, so it’s being rejected
The key to understanding this challenge is to understand that the object being passed as a parameter to the inner function half() can be destructered right up front.
The call to the function is made here:
console.log( third( waterLevels1 )); // should be the sum of (high + low) / 3.0
Understand that waterLevels1 is an object with several properties.
What we’ve always done before is accept the whole object as a parameter which is what challenge starts out with.
return function third( todaysWaterLevels )) { etc…
Note: the parameter “todaysWaterLevels” accepts the object “waterLevels1”.
(Part of the confusion of the challenge is name of the parameter of the inner function is the same as the object it receives; if the parm name had been different from the object name, it might have made more sense. Example:
return function half( anyObjectWithMinAndMaxProperties ) {…etc…
)
So, what the challenge is asking us to understand is that the inner function can receive JUST specifically chosen properties so long as the passed object has those properties)
So, instead of a function that receives an object like this:
return function third(todaysWaterLevels) {…etc…
… the function can receive just the specific properties that are wanted without caring about the name of the object coming in
return function third( {high, low} ) {…etc…
return (high + low) / 3.0;