Why do I need itemCount?


#1

Hello :slight_smile:
My question is - why do I need itemCount? I understand it’s a local variable, while orderCount is a global one. Why can’t I use only the orderCount like below? It seems to be working and there’s less code to write.

MY SOLUTION:
let orderCount = 0;
const takeOrder = (topping, crustType) => {
console.log('Order: ’ + crustType + ’ pizza topped with ’ + topping);
orderCount++;
};

const getSubTotal = () => {
return orderCount * 7.5;
}

takeOrder(‘mushroom’, ‘thin crust’);
takeOrder(‘spinach’, ‘whole wheat’);
takeOrder(‘pepperoni’, ‘brooklyn style’);

console.log(getSubTotal());

ORIGINAL SOLUTION:
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));


#2

i really like this stackoverflow answer:

https://stackoverflow.com/questions/10525582/why-are-global-variables-considered-bad-practice

but this would mean redesign the entire program.


#3

This answers my question entirely, thank you :slight_smile:


#4

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