Confusion in Functions Ex. 5 return


#1


https://www.codecademy.com/en/courses/learn-javascript/lessons/functions/exercises/return


I don't understand why the function getSubTotal requires the creation of a new variable "itemCount" when it has exactly the same information as "orderCount". Surely the variable "orderCount" could be passed in here with the same result?


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

function getSubTotal(itemCount) {
  return itemCount * 7.5;
}

takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');

console.log(getSubTotal(orderCount));


#2

orderCount is a global variable and can be used in both functions,itemCount is just a parameter and nothing more it can be substituted with x, y or any other name.


#3

Is there any advantage to using a different parameter? For me, using itemCount just makes the code more confusing, when orderCount would be more logical?


#4

Parameters can be anything you set them to be because they are only placeholders for the values you will put in your function call.itemCount and orderCount are not the same thing because orderCount is a global variable while itemCount is just a placeholder for orderCount.


#5

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