So I was doing exercise 6 and had a couple of questions which I’ve added in as comments. Any insight into it will be appreciated.
let orderCount = 0;
const takeOrder = (topping, crustType) => {
orderCount++;
console.log('Order: ' + crustType + ' pizza topped with ' + topping);
};
//why is itemCount being used as a parameter and why not orderCount?
let getSubTotal = (itemCount) => {
return itemCount * 7.5;
};
//why orderCount here? i know it gets added everytime takeOrder is called
const getTax = (orderCount) => {
return getSubTotal(orderCount) *0.06;
};
//why is the parameter left out here
const getTotal = () => {
return getSubTotal(orderCount) + getTax(orderCount);
};
takeOrder('mushroom', 'thin crust');
takeOrder('spinach', 'whole wheat');
takeOrder('pepperoni', 'brooklyn style');
//when calling a function like getSubTotal below do you have to add its parameter? or does it become an argument when added?
console.log(getSubTotal(orderCount));
console.log(getTotal())
the parameter has a local scope within the function. Given it the same name as a global variable becomes confusing, how do you know what is what then?
parameters are placeholders, they get there value from the argument at function call.
parameters aren’t mandatory for functions, in this case there is no parameter needed
personally, i disagree with the whole design used here. We both modify and use a global variable and we use it as argument on function calls. I would redesign the whole thing:
//why orderCount here? i know it gets added everytime takeOrder is called
const getTax = (orderCount) => {
return getSubTotal(orderCount) *0.06;
};
not giving getTax function any parameter and then just returning getSubTotal(orderCount) * 0.06 seems to work just fine, getting the same final result.