I recently completed the Function Expressions exercise. While I understand these are isolated exercises and serve only as specific learning points, I do not understand why we would call the function with its parameter value and then log it to the console, having to add the same parameter value instead of defining the parameter value in a variable so we only need to type it once, which would allow us to change it easily later on. Here is an example of what I mean:
let dayNeeded = ‘Wednesday’
const plantNeedsWater = function(day) {
if(day === ‘Wednesday’) {
return true;
} else {
return false;
}
}
plantNeedsWater(dayNeeded);
console.log(plantNeedsWater(dayNeeded));
In your example, the first function call doesn’t store its result, and since it’s a pure function (has no side-effects), it essentially does nothing (other than warm up the cpu a little). The console.log of the function call does write to the console IO, but that just serves as a test again, because you are not saving the calculation in any variable or using it for any calculation.
A more realistic use case after could look like
let dayNeeded = getTodaysDay();
if (haveWater(dayNeeded) && plantNeedsWater(dayNeeded))
{ //do something
}
1 Like
I somewhat get that it doesn’t do anything. but it seems to output the correct result. I am not sure I understand your example. I am doing a JavaScript course and am busy with function expression. Not sure how your example is used in a function expression?
plantNeedsWater
that you showed is a function defined through a function expression. My example is how you might use that function in the future. By not doing anything I don’t mean that it doesn’t calculate the right thing, but that without putting it to use it serves little purpose. I just added that to give you an idea of how it may get used.
1 Like