Why itemCount = takeOrder?


#1

Why the argument itemCount in the getSubTotal function is equale to the takeOrder function?


itemCount and orderCount
#2

can you clarify your question with code please? thank you


#3
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 = (orderCount) => {
 return getSubTotal(orderCount) * 0.06;
}

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


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

Here it is.


#4

by which you mean:

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

this piece of code?

equal to the takeOrder function? its not.

Could you clarify which pieces of codes are relevant to your question? Clarifying your question with code, doesn’t mean just dumping the code. What do you think equals?


#5

i mean how the computer knows that itemCount equales to the number of the times that the takeOrder function was called?


#6

I understamd that orderCount passes his value to itemCount but why?


#7

orderCount is passed as argument on function call, so the value of argument at parameter is passed to the function call


#8

Thank you for your answer! but i still don’t really understand how the getSubTotal function is related to the takeOrder function. I also experimented for a little bit and found out that is does not matter what the parameter inside the getSubTotal function is, the result would be the same, could you also explain me how that works?


#9

the getSubTotal function calculates the cost based on the number of orders:

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

/* function call with argument orderCount */
console.log(getSubTotal(orderCount));

see comments, because i am going to use those terms

when we declare a function, we can give the function a parameter. The parameter acts as placeholder. Then when the function is called we supply an argument, which gets passed to the function parameter.

This is possible because function don’t execute until called

so the parameter at function deceleration gets its value from argument at function call

This is how the values are associated, how come you don’t have a problem with this from the takeOrder function itself:

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');

given exactly the same thing with parameters and arguments is happening?


#10

Ok , i think i finally understand it. Thank you for being so ever patient with me. :grin:


#11

what makes it confusing is this line:

console.log(getSubTotal(orderCount));

you don’t need it, getSubTotal is called by other function, you only need to call getTotal() to make the program work


#12

yeah, i understand it now. thank you!


#13