Pizza.js orderCount


#1

I’m confused as how orderCount++ or orderCount = orderCount + 1 knows there is three orders when you call the takeOrder function 3 times. All it should know is that orderCount is still zero and to add 1 to it. Just because it’s inside of the takeOrder function how in the world does it know what to do?

Also the getSubtotal is a useless function, why not just create a variable and give it a value of 7.5?



let orderCount = 0;

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

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

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

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

const getTotal = () => {
  return getSubTotal(orderCount) + getTax(); 
}; 

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



#2

Hi @alenart
For orderCount = orderCount + 1;
everytime you call that function, (functional component), it increments the variable orderCount by 1
So therefore, orderCount becomes 3 since you called the func 3 times.

And subTotal is a very useful because it’s easier to call a function than do orderCount * 7.5 a lot of times.
The purpose of a function is to increase readability and to have an easy way to access a statement or expression without writing it out every single time.

Hope this helps :smiley:


#3

but the function is takeOrder, and everywhere in the code blocks is calling orderCount which is just nothing.

also for this line of code

console.log(getSubTotal(orderCount));

is this multiplying them?


#4

I think alenart may be confused by the fact that orderCount is basically a golbal variable (or whatever those are called in Javascript) but at the same time it’s used as an argument for functions that don’t need it as an argument because they can see the variable anyway, which makes no sense.
It would be okay-ish if it was done consistenly but functions like

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

which only work because orderCount can be seen everywhere in the little program but then use it as an argument for the inner function call are just completely confusing, no matter if you know what scope is or not.


#5

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