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