5. Functions - Return : itemCount explained?


#1

Hi there.

Can’t seem to get my head around the use of the itemCount parameter in the code:

let orderCount = 0;
const takeOrder = (topping, crustType) => {
  console.log('Order: ' + crustType + ' pizza topped with ' + topping);
  orderCount++;
}
const getSubTotal = (itemCount) => {
  return itemCount * 7.5;
 }

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

console.log(getSubTotal(orderCount));

I don’t understand where the argument for itemCount is passed from?

In the takeOrder function, the parameters for ‘topping’ and ‘crust’ are passed (in the first order) the arguments ‘mushroom’ and ‘thin crust’.

Now I understand that the getSubTotal function gets the parameter itemCount. But where is the argument (which in this case is obviously ‘4’ ) passed from?

The only value I can see of ‘4’ in the code is the final orderCount, once all the functions have been called. I don’t however, see where this is part of the function.

(Apologies for almost duplicating previous questions about this, but having read the assorted answers, I’m still not grasping it.)

Many thanks.


#2

The latter is the function call, where the argument is orderCount. This is received by the function as the parameter, itemCount.


#3

Ahhhhhh! A light bulb switches on I think.

So in that code, topping is to mushroom as itemCount is to orderCount for their respective functions?

Just so I have the nomenclature correct; if the argument ‘mushroom’ is a string, is there a specific term for the variable orderCount as it is used here, or all are just arguments?

Many thanks for the quick reply by the way.


#4

Because data types are not declared explicitly in JS, the parameter takes its data type from the argument passed to it.

If orderCount is an integer, itemCount will be that integer.

takeOrder('anchovies', 'thin')

The call is where the type is defined for the parameters of the function.


#5

Understood.

Thanks a million!

Onward and upwards.


#6

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