Learn JavaScript : Problems understanding how the Code works


#1


Learn Java Script 6/7 Pizza Order


My Code behaves correctly, but I have problems understanding the connection between orderCount and the multiplication in itemCount. If I understand it correctly, the program counts how many pizzas are ordered in the first function "orderCount = orderCount + 1". But I don't understand how the number in orderCount is transferd to the second function, in which we calculate the SubTotal(itemCount). In the second function the variable orderCount is not mentioned.

Can someone please explain, how the program gets the information in the second function? Thank you!!

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;
}


Replace this line with your 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;
}

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());


#2

The orderCount variable is a global variable meaning it can be used in both functions, and any changes made to it are carried on to the next line it is referred to,

itemCount is just a parameter meaning the result that you get back depends on the value that you put in the function call. :slight_smile:


#3

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