How did the answer come as 22.5


#1


var orderCount = 0;

function takeOrder(topping, crustType) {
  console.log('Order: ' + crustType + ' crust topped with ' + topping);
  orderCount = orderCount + 1;
}

function getSubTotal(itemCount) {
  return itemCount * 7.5;
}

takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');

console.log(getSubTotal(orderCount));






The answer in the console shows 22.5. I want to know how??


#2

3 * 7.5 is 22.5, itemCount is 3, because you order 3 items


#3

I wondered how the program knew what is the value of itemCount. From what I understand, we have defined only orderCount, which increases every time new order is placed (three times in above example). How did it know what to do with itemCount and how did it come to its value of 3?

Thanks


#4

well, orderCount starts off as zero:

var orderCount = 0;

then we call takeOrder 3 times:

takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');

each function call will execute the function, which means this line of code is reached:

orderCount = orderCount + 1;

which will increase orderCount.

then we call getSubTotal:

console.log(getSubTotal(orderCount));

with orderCount as argument, the argument gets copied into the function parameter (itemCount) which you multiply by 7.5


#5

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