Parameters and arguments - Why is it not the exact same as declared?


#1

I am having trouble understanding this code.

Step 5 says, "getSubTotal has a parameter that represents the amount of items ordered. Pass in the orderCount as an argument when making the function call."

console.log(getSubTotal(orderCount));

But orderCount is not the correct argument. Shouldn't it be itemCount, because that is what was declared in the function i.e.

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

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

Here is the full piece of code:


var orderCount = 0;
function takeOrder(topping, crustType) {
  console.log('Order: ' + crustType + ' crust topped with ' + topping);
  orderCount === orderCount + 1;
}
function getSubTotal(itemCount) {
  return itemCount * 7.5;
}
takeOrder('bacon', 'thin');
takeOrder('cheese', 'normal');
takeOrder('salami', 'thick');

console.log(getSubTotal(orderCount));

If I am wrong, could someone please explain why? How is it possible to use an argument that was not declared in the function??

Thanks


#2

No. itemCount does not exist in the scope where this call is made. orderCount is the global that has been accumulating. itemCount is local, only.

Continuing,

An argument is a value or expression. JavaScript function objects have an arguments object that receives the values passed by the caller. The parameters are arbitrary names we give the items in the arguments object.

Looked at from another angle, the order in which the arguments are stacked is not dependent upon the order of the parameters, but the order of the arguments. The parameters are assigned in sequence, locally.


#3

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