const prepareGreenTea = () => ‘greenTea’;
const prepareBlackTea = () => ‘blackTea’;
const getTea = (prepareTea, numOfCups) => {
const teaCups = [];
for(let cups = 1; cups <= numOfCups; cups += 1) {
const teaCup = prepareTea();
teaCups.push(teaCup);
}
return teaCups;
};
// Add your code below this line
const tea4GreenTeamFCC = null; // 
const tea4BlackTeamFCC = null; // 
// Add your code above this line
console.log(
tea4GreenTeamFCC,
tea4BlackTeamFCC
);
I tried changing:
const teaCup = prepareTea();
** teaCups.push(teaCup);**
to: **
_ teaCups.push(prepareTea);**_
**And instead it returns the whole function as opposed to the single value. **
Why does that happen, I thought it would be the same.
Example:
function sayHello() {
return "Hello";
}
This is the name the function is assigned to, so if you check what sayHello
is, it will be a function:
> sayHello
[Function: sayHello]
This is how you execute the function (evaluate it to a value), so if you check what sayHello()
is, it will be “Hello”:
> sayHello()
"Hello"
Thank you very much, I understood.
I now changed it to:
teaCups.push(prepareTea());
And it works fine.
Is there any reason we should use the original way by declaring a const first instead of using the method directly?
Bear in mind that this is a contrived example: it doesn’t really do anything that couldn’t be done, as you say, by using it directly. It’s more trying to get you used to splitting things out — in reality, it might be something a lot more complex. And when it’s split out into a separate function, it means you can test that function on its own to check it does the thing you want it to do
1 Like