Question on this code for 6/10 Return II


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

https://www.codecademy.com/courses/learn-javascript-functions/lessons/functions/exercises/return-ii?action=lesson_resume&course_redirect=introduction-to-javascript

Hey all, so I finally got this code correct and passed the exercise, however I’m looking for a better understanding of one thing that I’m confused on. Why exactly do I need to include the argument “orderCount” for the getSubTotal functions when I call them in my “getTax” and “getTotal” blocks. I’m confused because I don’t need an argument passed into the getTax function, so why is it needed in the getSubTotal function? just specifically in my code for the getTax and getTotal blocks.



let orderCount = 0;

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

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

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

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

takeOrder('mushrooms', 'thin crust');
takeOrder('pepperoni', 'seasoned crust');
takeOrder('xtra cheese', 'cheese stuffed crust');

console.log(
  `Subtotal: $${getSubTotal(3).toFixed(2)}`);

console.log(
  `Total with tax: $${getTotal().toFixed(2)}`);

#2

lets do an example, here are two functions:

const firstExample = () => {
    console.log("no parameter so just logging pointless string to console");
}
const secondExample = (exampleParameter) => {
    console.log(`the value of the parameter is: ${exampleParameter}`);
}

the first function doesn’t have a parameter. As such, when i call the function i don’t need to supply an argument:

firstExample();

however, the second function does have a parameter, so it needs an argument:

// function declaration with parameter exampleParameter
const secondExample = (exampleParameter) => {
    console.log(`the value of the parameter is: ${exampleParameter}`);
}
// function call with string argument
secondExample('hello world');

if we do not supply an argument at function call, the value of the parameter would be undefined:

const secondExample = (exampleParameter) => {
    console.log(`the value of the parameter is: ${exampleParameter}`);
}
secondExample();

which is something we do not want for our pizza program.

normally, as a programmer you decide if your function should or shouldn’t parameters, at the moment the exercises take this decisions for you

important to understand: functions only execute when called, as such, a function parameter is a placeholder until the parameter gets its value from argument at function call

hope this helps :slight_smile:


#3

Yes, that definitely makes more sense! I feel like I probably should have realized that. Thanks!


#4

well, what makes it confusing is having functions calling other functions, which is why i simplified it a bit for the explanation :slight_smile:

No more questions left?


#5

No I’m good now thanks


#6