1

Apologies for the ambiguous title, but it's hard to explain what I want in one sentence. I'll do my best to give you an idea of what I'm trying to do. I'm writing a simple program that will return what pizza has been ordered, the price of the pizza(s) without tax and the price of the pizza(s) with tax included. Here's the code for it:

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

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

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

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

takeOrder('bacon', 'thin');
console.log(getSubTotal(orderCount));
console.log(getTotal());

takeOrder('sausages', 'thick');
console.log(getSubTotal(orderCount));
console.log(getTotal());

takeOrder('olives', 'medium');
console.log(getSubTotal(orderCount));
console.log(getTotal());

As you can see, the price of the pizza is always the same regardless of the topping and the crust. It's always 7.5. But now I want to make three different toppings (as used before): bacon, sausages and olives with different prices. This means that the price of the pizza should change depending on what topping is used for the pizza. I tried this code, but it didn't work:

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

function getSubTotal (itemCount){
	if (topping === 'bacon') {
  return itemCount * 7.5;
  }
  else if (topping === 'sausages'){
  return itemCount * 6;
  }
  else if (topping === 'olives'){
  return itemCount * 5.5;
  }
  else {
  return ('Topping not available.');
  }
}

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

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

takeOrder('bacon', 'thin');
console.log(getSubTotal(orderCount));
console.log(getTotal());

takeOrder('sausages', 'thick');
console.log(getSubTotal(orderCount));
console.log(getTotal());

takeOrder('olives', 'medium');
console.log(getSubTotal(orderCount));
console.log(getTotal());

I want to do the same for crustType (change prices depending on the type of crust), but for now I just want to figure out the toppings. Would appreciate if someone could explain what I'm doing wrong since I'm new to JavaScript.

Thank you

0

3 Answers 3

1

You have to save the topping.

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

function getSubTotal (itemCount){
	if (topping === 'bacon') {
  return itemCount * 7.5;
  }
  else if (topping === 'sausages'){
  return itemCount * 6;
  }
  else if (topping === 'olives'){
  return itemCount * 5.5;
  }
  else {
  return ('Topping not available.');
  }
}

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

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

takeOrder('bacon', 'thin');
console.log(getSubTotal(orderCount));
console.log(getTotal());

takeOrder('sausages', 'thick');
console.log(getSubTotal(orderCount));
console.log(getTotal());

takeOrder('olives', 'medium');
console.log(getSubTotal(orderCount));
console.log(getTotal());

Sign up to request clarification or add additional context in comments.

Comments

1

When you call takeOrder, you are passing topping information but you are not storing it anywhere.

So when you call getSubTotal, the variable topping is found to be not defined and you must be getting Uncaught ReferenceError.

You need to declare variables

var topping, crust;

just like orderCount. so it should look like:

var orderCount = 0,
    topping, crust;

Also, since you need to store the values into them when you call takeOrder function, you need to modify the function like this:

function takeOrder(toppingType, crustType) {
 console.log('Order: ' + crustType + ' pizza topped with ' + toppingType);
 orderCount = orderCount + 1;
 topping = toppingType;//store the passed value here
 crust = crustType;//store the passed value here
}

Then it should work.

Comments

1

As mentioned above, topping is an undefined variable.

Another issue is that here:

takeOrder('bacon', 'thin');
console.log(getSubTotal(orderCount)); // <<<<
console.log(getTotal());

you are multiplying the price of a specific topping by the total amount of all pizzas ordered regardless of their toppings. So if you ordered three bacon pizzas and one olives pizza you'd be multiplying the price for olives by 4 (the total number of pizzas) when you should just be multiplying it by one.

What you could do is store the prices of toppings in an object and keep a running pre-tax sub-total that gets added to every time you place an order. Then when you call getTax and getTotal these functions will use that running sub-total to calculate the value of their output.

var menuItems = {
  bacon : 7.5,
  sausages : 6,
  olives : 5.5
}

var subTotal = 0;

function takeOrder (topping, crustType) {
  console.log('Order: ' + crustType + ' pizza topped with ' + topping);
  subTotal += menuItems[topping];
}

function getSubTotal() {
  return subTotal
}

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

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

takeOrder('bacon', 'thin');
console.log(getSubTotal());
console.log(getTotal());

takeOrder('sausages', 'thick');
console.log(getSubTotal());
console.log(getTotal());

takeOrder('olives', 'medium');
console.log(getSubTotal());
console.log(getTotal());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.