Functions-basic


#1

I just understand why the following line code uses (itemCount) rather than orderCount? And why the instance is used once only.

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

function getTotal () {
return getSubTotal(orderCount) + getTax();
}


#2

I think that is mostly a design decision. And the purpose of that is to make the code look more logical, flexible, and less confusing (but that seems to have backfired since you're confused).

Consider this, a function should have parameters that should take any variables. Why make the function's parameter fixed to only one variable when you can possibly use that function for another input, say var orderCount2 = 78988;?

itemCount was just a name for the parameter that can be replaced with an actual variable or value. As you can see, it has been replaced with orderCount later on.

topping and crustType serve the exact same purpose as itemCount. Your question is essentially asking why do we use topping instead of mushroom if there ever was a variable defined called mushroom? Because there is a variety of toppings and not just mushrooms. Same concept with itemCount, it can be any count.


If you change it to orderCount, I think it still works because of something called function scoping but I wouldn't recommend it. Context is just as important and doing that will muddle it.