Question about an exercise in general


#1


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


How do "console.log(getSubTotal(orderCount));" and "console.log(getTotal());" from the code below
result 22.5 and 23.85? Can somebody please explain?


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('sardines', 'regular');
takeOrder('pepperoni', 'deep dish');

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


#2

First, you have a method takeOrder which registers all new orders.
orderCount registers +1 pizza for every new order.

Then we know that the price of one pizza is 7.5.

Since you ordered three pizzas...

takeOrder('bacon', 'thin');
takeOrder('sardines', 'regular');
takeOrder('pepperoni', 'deep dish');

... you multiply the itemCount (number of orders – 3 in this case) by the price of one pizza (7.5) :

7.5 * 3 = 22.5 that's your total price for 3 pizzas (getSubTotal(orderCount)

But don't forget the tax (which is 0.06) ! You can multiply 22.5 * 0.06 which will give 1.35

Add 22.5 to 1.35 and that's your final price (getTotal) of 23.85


#3

Oh I get it now. Thank you.
But I always thought "var orderCount = 0;" as the number of ordered pizza. So what is the exact usage of this variable?

Also, if I want to order 20 pizza, do I have to type "takeOrder(...)" twenty times?


#4

But I always thought "var orderCount = 0;" as the number of ordered pizza. So what is the exact usage of this variable?

You own a pizzeria and you're starting your day.
You obviously haven't had any orders yet for the day, but are ready to take orders now.

Your start your day with a number of orders equivalent to 0. No orders yet.
Say you don't own a computer system, you need to log everything in a book (keep track of all orders, etc).
You start a new day with a blank page, right? Well that's exactly what this variable is about. No orders yet.


Also, if I want to order 20 pizza, do I have to type "takeOrder(...)" twenty times?

Whether you have a computer system or a book to keep track of your orders, each order is its own entity, and you will want to log each order separately.

I like my pizza with Mozzarella, Olives and Anchovies. That's one record.
You might like a completely different kind of pizza. That's another, separate, unique record.

So, yes, each order takes a new line, takeOrder, with the specs of the pizza.


#5

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