Problem with an exercise in JavaScript


#1

var orderCount = 0;

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

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

function getTax() {
return getSubTotal(orderCount) * 0.06;
}

function getTotal() {
return getSubTotal(orderCount) + getTax();
}

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

console.log(getSubTotal(orderCount));
console.log(getTotal());

This is the code codecademy gave me! I understand everything, but why does it always say "getSubTotal(orderCount)" and not with out the words in the brackets... Hopefully someone understands me, because I am a german and my english isn't so good :wink:

Thanks


#2

Can someone explain parameters again too, because I think this is the problem, why I don't understand the problem... :slight_smile:


#3

var orderCount = 0;

This variable is known as a global and is purposely outside of any function so that it can be modified by any of them. We see it accumulates (increments) every time an order is placed.

The getSubTotal function takes the value represented by this variable as an argument. A parameter is the variable that contains the argument passed into the function.

function function-name (parameter) {
    // code block (function body)
    // do something with parameter variable value
}

A call to invoke the function consists of the function name, and an argument list in parens.

function-name(argument);

Notice we pass in the current value of orderCount and return that value multiplied by 7.5, the price of one order. If there are five orders placed, this expression will be,

return 5 * 7.5      => 37.50

We need this total to calculate the tax, which is why getTax calls the getSubTotal function, and calculates the tax, which is the value returned.

return 37.50 * 0.06  => 2.25

The only program we need to call, though, is getTotal. It calls both functions and adds the two results together to get a total.


#4

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