Functions lesson 6.Return II


#1

Hi all,
Can someone help explain what is going on in this code?

let orderCount = 0;

const takeOrder = (topping, crustType) => {
  orderCount++;
  console.log('Order: ' + crustType + ' pizza topped with ' + topping);
};

takeOrder('mushroom', 'thin crust');
takeOrder('spinach', 'whole wheat');
takeOrder('pepperoni', 'brooklyn style');

const getSubTotal = (itemCount) => {
  return itemCount * 7.5;
};

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

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

console.log(getSubTotal(orderCount));
console.log(getTotal());

I get what is happening up to the

const getSubTotal = (itemCount) => {
  return itemCount * 7.5;
};

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

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

console.log(getSubTotal(orderCount));
console.log(getTotal());

portion.
For

const getSubTotal = (itemCount) => {
  return itemCount * 7.5;
};
  1. What is itemCount used for? It is not used anywhere else in the code so what does it contain? I understand that the function is calculating a subtotal but what are the values contained in itemCount its self? I tried to print out its value but the program just errors out.

  2. What is held in getSubTotal(orderCount)? It never had a value passed to it, getSubTotal had itemCount*7.5 passed it.

Thank you for helping to answer my questions in advance. :grin:


#2

itemCount is a parameter, so it gets its value from argument at function call:

// function decleration with parameter itemCount
const getSubTotal = (itemCount) => {
  return itemCount * 7.5;
};

// function call with argument orderCount
getSubTotal(orderCount)

see comments.

so orderCount (which contains the number of orders) is passed to the function parameter (itemCount) when the function is called.

function parameters are variables, but they have a local scope, the only exist within the function.

the confusing thing in your program is, that getSubTotal function call is within another function (getTax and getTotal)

which is also the reason we have the getSubTotal function, in programming, DRY (don’t repeat yourself) is a very important principle), and give we need to use getSubTotal twice, we made a function out of it.

Yes, we could calculate getTotal in a single go, but doing it the way you do, makes our program more modular. Which is good practice, although it might make things a bit confusing at first


#3

Thanks @steim94,
Sorry, maybe it’s how I worded the question. Can you point out what line in the code we pass orderCount to itemCount? That is what I am stuck on right now.

The code provided is what is given when I selected the “Get Code” option. I got stuck on the instructions
“Inside the getTax() function’s block, call your getSubTotal() function to get the subtotal and then multiply the returned value by 6% (0.06). Make sure to return the result of this operation.”
I originally took these instructions as below since I thought that now contained the subtotal amount. But that gave an error.

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

I think I get what the “getSubTotal = (itemCount)” portion is doing now, thank you for the explanation. It is passing itemCount*7.5 to subTotal, correct?


#4

That is the line that passes the global as an argument. The parameter is local identifier of that value.


#5

Does this mean that orderCount is considered itemCount now?


#6

It’s value is represented by itemCount, but only inside the function. Nothing happens to the global.


#7

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