getTax() parameter

Hey everyone.

I’m currently on the Pizza program and came across the various functions used like getSubTotal().
While I understand the requirement for certain functions to have parameters associated to them, I can’t figure out why getTax() needs a parameter like orderCount associated to it.

As far as I understand, all getTax() does is multiply getSubTotal(orderCount) with 0.06, so it wouldn’t need to call a specific variable for this calculation, since getSubTotal() has taken care of that requirement. I’ve even tested the code without giving getTax() any variables and it works fine.

Could anyone explain if there’s any reason behind giving a parameter to getTax()?

Could you post a link to the lesson, and your code, so we can see what you’re talking about?

I think you just said it yourself. It needs to get orderCount from somewhere, so you should give the function that value when you call it.

// poorly behaved function, ignores arguments
return a + b;
}

let a = 7, b = 5;
console.log(add(3, 4));  // I expect to see 7, not 12

Oh shucks! Sorry for that. Here it is:

let orderCount = 0;
const takeOrder = (topping, crustType) =>{
console.log(Order: \${crustType} pizza topped with \${topping});
orderCount++;
};
const getSubTotal = (itemCount) =>{
return(itemCount * 7.5);
}
const getTax = () =>{
return getSubTotal(orderCount) * 0.06;
}
const getTotal = () =>{
return(getSubTotal(orderCount) + getTax());
}
takeOrder(‘bacon’, ‘thin crust’);
takeOrder(‘pepperoni’, ‘thick crust’);
takeOrder(‘secret sauce’, ‘special’);
console.log(getSubTotal(orderCount));
console.log(getTotal());

Link to the lesson page is here:

I see your point, but then shouldn’t the getTotal() function also have a parameter associated to it? I’ve tried running the program without giving getTax() a parameter, and it works fine. Here’s the code for your reference:

let orderCount = 0;
const takeOrder = (topping, crustType) =>{
console.log(Order: \${crustType} pizza topped with \${topping});
orderCount++;
};
const getSubTotal = (itemCount) =>{
return(itemCount * 7.5);
}
const getTax = () =>{
return getSubTotal(orderCount) * 0.06;
}
const getTotal = () =>{
return(getSubTotal(orderCount) + getTax());
}
takeOrder(‘bacon’, ‘thin crust’);
takeOrder(‘pepperoni’, ‘thick crust’);
takeOrder(‘secret sauce’, ‘special’);
console.log(getSubTotal(orderCount));
console.log(getTotal());

That comes down to whether or not those functions are meant to share variables or not. Maybe everything is meant to be shared, maybe only some things. Consider what they are meant to do. is getTax supposed to look at some global state, or is it supposed to accept a number and spit out another, completely independently from everything else?

In general, information should be shared by passing messages, rather than passing messages by sharing information. The former means that each part makes sense on its own, the latter means everything depends on everything else and you really can’t tell whether anything is correct or not unless you understand the full program perfectly (which is easier to do by looking at a single part at a time)

1 Like

In this instance, all getTax() does is perform a calculation on an already existing value, i.e. Multiply the value of getSubTotal(orderCount) with 0.06. The same is done by getTotal() by adding getTax() and getSubTotal(). From what I’ve understood, they seem independent of whether a parameter is set or not.

So, it makes sense that getTotal() doesn’t have a parameter, so then why should getTax()?

It doesn’t make sense for getTotal to not take an argument. There is no sensible place for it to get the order count. It also doesn’t make sense for takeOrder to modify a variable outside itself like it does, that isn’t a sensible place to store that information.

What would make sense is to create a new order:

order = new PizzaOrder();
console.log(order.toString());
console.log(order.getTotal());

In this way, you create an order and ask it to modify itself, global state is left untouched and you can have multiple orders being created at the same time without interference.

Because the object knows its orders, one won’t need to tell it how many orders there are when asking for the total.

Alternatively one could do something like starting out with an empty array and passing it into a function along with a new order, and get a new array back which you can pass in again to add more orders, or to pass in to a getTotal function. But having an object take care of its own information storage and bundle up its behaviour as methods makes it easier to use

Oh and even if one does have some tiny script sharing global state between functions, there’s nothing saying there can’t be self-contained functions which don’t get involved in that mess.

Hey ionatan, thanks for the replies to try and help me understand. Unfortunately, I just began learning JavaScript only a few days back, so I do not understand the alternative explanation which you just gave.

Here’s a link to the lesson I’m currently on: