# Return 2 . Can someone please explain how this works?

#1

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
I can’t understand why the argument of the orderCount is 3. I know I called the takeOrder function 3 times, but how does the computer know I called the function 3 times?
In the code, initially the value of orderCount is 0. and then I added 1 to orderCount, so now the value of orderCount is 1.
So I expected the subTotal would be 7.5 (because 7.5 * 1 = 7.5), and actually the subTotal is 22.5 which is 7.5 * 3.

Can someone explain how this happened?

I hope you understand what I mean. Thank you

``````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);
}

function getTax() {
return getSubTotal(orderCount)*0.06;
}
takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');
console.log(getSubTotal(orderCount));
``````

And this is the result :

``````Order: thin crust topped with bacon
Order: regular crust topped with pepperoni
Order: thin crust topped with pesto
22.5
``````

#2

Hi @yerimmm,

I’ve added some comments into the code to aid the explanations, do have a look:

``````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);
}

function getTax() {
return getSubTotal(orderCount)*0.06;
}

takeOrder('bacon', 'thin');  //<=== function takeOrder being called for 1st time

takeOrder('pepperoni', 'regular');  //<=== function takeOrder being called for 2nd time

takeOrder('pesto', 'thin');  //<=== function takeOrder being called for 3rd time

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

``````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);
}

function getTax() {
return getSubTotal(orderCount)*0.06;
}

takeOrder('bacon', 'thin');    //<=== computer run function takeOrder for 1st time, in the end, add 1 to orderCount, orderCount updated to 1

takeOrder('pepperoni', 'regular');  //<=== computer run function takeOrder for 2nd time, this time, orderCount = 1, add 1 to orderCount, orderCount updated to 2

takeOrder('pesto', 'thin');  //<=== computer run function takeOrder for 3rd time, this time, orderCount = 2, add 1 to orderCount, orderCount updated to 3

console.log(getSubTotal(orderCount));  //<=== at this time, orderCount value is updated to 3, so orderCount = 3 when this part of code is running.
// Continue: Only at this period, function subTotal is called. Therefore,  the subTotal is 22.5 which is 7.5 * 3.
``````

This happened because you called the subTotal in the very last line, at that time, orderCount is 3. So, it is 7.5 * 3.

Another note, if you write your code this way, you will notice the subTotal is different everytime.

``````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);
}

function getTax() {
return getSubTotal(orderCount)*0.06;
}

takeOrder('bacon', 'thin');
console.log(getSubTotal(orderCount));

takeOrder('pepperoni', 'regular');
console.log(getSubTotal(orderCount));

takeOrder('pesto', 'thin');
console.log(getSubTotal(orderCount));
``````

And it will print:

``````Order: thin crust topped with bacon
7.5
Order: regular crust topped with pepperoni
15
Order: thin crust topped with pesto
22.5
``````

Hope this helps.

#3

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