Javascript Functions : "orderCount" and "itemCount" variables


#1

In the Functions sections of the Intro course the JavaScript there is a part where you end with this code while learning about “return”.

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');
console.log(getSubTotal(orderCount));

Can someone explain to me the correlation between “itemCount” and “orderCount”? where does itemCount get its value and why wouldn’t it be “return orderCount * 7.5” and so on? Thanks in advance!


#2

In the above call we pass the global as the argument, which becomes itemCount locally in the function.


#3

So, you mean since ‘orderCount’ is a global variable it can be passed in a local variable as argument, changing the value of the local variable’s argument?


#4

We can use the global as an argument which we pass to the local parameter. The parameter will point to the same object, but if it is changed inside the function, it will no longer point to the global object (unless the object is an Array or Object).


#5

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