Lesson 6 Return II - why is parameter included again?


#1

const getTax = (orderCount) => { return getSubTotal(orderCount) * 0.06; }
const getTotal = () => { return getTax(orderCount) + getSubTotal(orderCount); }

If orderCount is already calculated in the const getTotal and getTax,
why do we need to include it in return getTax(orderCount) + getSubTotal(orderCount);

Why not just
const getTotal = () => { return getTax() + getSubTotal() };


#2

go ahead, try and see what happens.

Lets first clarify something, if we declare a function:

/* function declaration with parameter orderCount */
const getTax = (orderCount) => {
   /* function content */
}

we can decide to give it a parameter orderCount. Now when we call the function:

/* function call with argument */
getTax('argument');

we need to supply an argument, otherwise the function parameter will be undefined, which we can see:

/* function declaration with parameter orderCount */
const getTax = (orderCount) => {
   /* logging value of parameter to console */
   console.log(orderCount); /* should log undefined */
}

/* function call with no argument despite function having a parameter */
getTax();

now, what complicates matters is that you have function calls within the body of a function.

I simplified the examples a bit, and hope you can use this knowledge to see why on this line:

const getTotal = () => { return getTax(orderCount) + getSubTotal(orderCount); }

you do need to supply arguments to the function call. (getTax and getSubTotal are function calls!)


#3

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