Lesson 6 - I need some help understanding how and where to use Parameters


#1

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())

#2

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:

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

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

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

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

const main = () => {
  let orderCount = 0;
  orderCount = takeOrder('mushroom', 'thin crust', orderCount);
  orderCount = takeOrder('spinach', 'whole wheat', orderCount);
  orderCount = takeOrder('pepperoni', 'brooklyn style', orderCount);
  console.log(getSubTotal(orderCount));
  console.log(getTotal(orderCount))
}
main();

so we don’t have this problem.


#3

+1 on this:

//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.

So I’m confused as to why it’s needed there.


#4

This has always been a problem with this exercise.

Updating global variable is a bad practice:

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

Why do you think i rewrote the entire program? See my previous reply.


#5

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