Functions: Returns II: Pizza order


#1


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


The code I have is the correct code, and runs, however it took me a while to figure it out and I still don't understand why the function getSubtotal(itemCount) uses itemCount as a parameter instead of orderCount. I also would like to know why the functions getTax and getTotal use getSubtotal(orderCount) with orderCount as a parameter. I guess I'm confused as to why there's a difference, and why the program won't run with itemCount as parameters, but the code will run if I change the parameter of the actual getSubTotal function to be orderCount. I know it's confusing. I commented my questions in the actual code for more clarity. You might also have to scroll to the right to see the whole comments. Thanks.


var orderCount = 0;

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

function getSubTotal(itemCount){ //Why do we use "itemCount" as a parameter and not just orderCount? Both work.
    return itemCount * 7.5;
  }

function getTax(){
  return getSubTotal(orderCount) * 0.06; //Why isn't this "return getSubtotal(itemCount)"?
}

function getTotal(){
  return getSubTotal(orderCount) + getTax(); //Why isn't this "return getSubtotal(itemCount)" = getTax();?
}

takeOrder('thin crust','bacon');
takeOrder('thick crust', 'pepperoni');
takeOrder('thin crust', 'garlic');

console.log(getSubTotal(orderCount)); //Why isn't this "getSubTotal(itemCount)"?
console.log(getTotal());


#2

Actually I think I get it. itemCount is the parameter of getSubTotal and orderCount is the value of the parameter? Wow, since the parameter wan't a number, and instead a variable, I was confused. If I'm right or wrong, please let me know. I think that's it though.


#3

You're right, itemCount is the name that gets given to any variable supplied to the getSubtotal function. The part that makes it confusing is that they didn't have every function take an itemCount variable, instead some functions rely on a global variable orderCount to do their work.

Its a result of building an exercise set from small steps but for a person like yourself who seeks deeper meaning it can cause confusion because you're right, the end result is a garbled mess of function patterns.


#4

Hey i think there is a mistake in writting . Wherever you see itemCount replace it with orderCount . itemCount is not defined and how program could know that there are 3 orders if you use itemCount ,and you can see that in line 3 you have this :

orderCount = orderCount + 1 , there is no itemCount .

Or maybe i made mistake . I got confused too . Someone help . :face_with_head_bandage:


#5

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