Function parameter question


#1

In the 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(‘pepperoni’, ‘regular’);
takeOrder(‘pesto’, ‘thin’);
console.log(getSubTotal(orderCount));

How does the parameter (itemCount) get it’s value of three In the function getSubTotal? I mean, how does itemCount know what it is doing? How does it ‘know’ what constitutes an ‘item’ , for example?


#2

You gave it that number.
Parameters are you telling a function it will be handed something. You write in the parenthesis what it is being handed for your own readabilities sake. In this case youre telling the function "Im going to hand you a certain number of items, take that number multiply it by 7.5 and give it back.

check out this line console.log(getSubTotal(orderCount));
You told the function to take however many items there are in orderCount and multiply it by 7.5
Remember that you determined how big orderCount was with the takeOrder function.

See how everything is coming together now? Functions hand each other data and manipulate it.


#3

Thank you for your reply. I am stupid.


#4

Nope, you’re learning. Which is the smartest thing of all. /cliche but true


#5

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