Syntax on Learn JavaScript:4. functions page 6/10


#1

Question: The code I came up with is this:


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

But the code being accepted is:


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

WHat is the difference? If we already have getSubTotal and getTax, why must they be recalled?


#2

Should those both be function calls?


#3

there is a difference between declaring a function and executing (calling) the function. What do you define as recalling?


#4

Here is the completed code:

let orderCount = 0;
const takeOrder = (topping,crustType) => {
orderCount ++;
console.log('Order: ’ + crustType + ’ pizza topped with ’ + topping)}
const getSubTotal = (itemCount) => {
const getSubTotal = (itemCount*7.5);
return getSubTotal;
}
const getTax = (orderCount) => {
return getSubTotal(orderCount)*0.06;
}
const getTotal = () => {
const getTotal = getSubTotal(orderCount) + getTax(orderCount);
return getTotal;
}
takeOrder(‘mushrooms’, ‘thin’);
takeOrder(‘hawaiian’, ‘stuffed’);
takeOrder(‘Bacon’, ‘gluten free’);
console.log(“Your total is $” + getTotal());

They are both functions that have stored values if I understand this correctly. If that is correct, then I don’t understand why I would have to call them a second time?


#5

let orderCount = 0;
const takeOrder = (topping,crustType) => {
orderCount ++;
console.log('Order: ’ + crustType + ’ pizza topped with ’ + topping)}
const getSubTotal = (itemCount) => {
const getSubTotal = (itemCount*7.5);
return getSubTotal;
}
const getTax = (orderCount) => {
return getSubTotal(orderCount)*0.06;
}
const getTotal = () => {
const getTotal = getSubTotal(orderCount) + getTax(orderCount);
return getTotal;
}
takeOrder(‘mushrooms’, ‘thin’);
takeOrder(‘hawaiian’, ‘stuffed’);
takeOrder(‘Bacon’, ‘gluten free’);
takeOrder(‘Bacon’, ‘gluten free’);
takeOrder(‘Bacon’, ‘gluten free’);
console.log(“Your total is $” + getTotal());

The two functions were called earlier and returned values. Recalling I define as giving them an updated variable, in this instance. IE they were already called and received values when they were created, so I’m not understanding why I must insert (ordercount) for each function.


#6

That looks a little bit off. How about,

return itemCount * 7.5;

?


#7

That part returns the same values. Am I understanding the return feature properly? The “return” at the bottom assigns a value to the variable in question?


Return II - questions
#8

When getSubTotal is called with orderCount as the argument, the return value, itemCount * 7.5 is what the caller sees.

Say orderCount is 3, then itemCount will be 3.

console.log(getSubTotal(orderCount));    // 22.5

#9

I am so baffled by this:

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

But then why does getTotal return getSubTotal(orderCount) and not getSubTotal(itemCount) as above?


#10

itemCount is the parameter of the getSubTotal function, which means it has a local scope:

const example = (someParameter) => {
   console.log(someParameter);
}

then when we call the function we supply a value for the parameter (in my example the parameter is named someParameter):

// function call

example("some argument for parameter");

if we tried to use the parameter outside the function:

const example = (someParameter) => {
   console.log(someParameter);
}
// function call
example("some argument for parameter");

// this will cause an error
console.log(someParameter);

because someParameter has a local scope, it only exist in the example function


#11

…and orderCount is defined outside the function so it can be used in anything below. Got it! Thanks for the answer!
This means that the function getSubTotal is supplied by a parameter that exists only locally but when used later in the getTotal function it is called already with the calculation done before by the return thingy, right?


#12

yes, orderCount has a global variable scope. So it can be used as argument on function call

yes, getTotal needs to calculate subtotal and tax before it can return the sum of those two values.


#13

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