6. Return II Pizza Order Extra Credit


#1



For this exercise, I got a little carried away and wanted to format the numbers that spit out for Sub-Total, Tax, and Total like they would show up on a receipt, i.e.:

Sub-Total: $XX.XX
Tax: $XX.XX
Total: $XX.XX

The below is how I accomplished this with only the knowledge I have so far from going through the exercises on this website. However, I'm sure there are more elegant ways of accomplishing the same output.

Feel free to comment on the code I came up with or share a simpler/more efficient method for showing the same format.


var orderCount=0;

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

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

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

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



takeOrder ('bacon', 'thin crust');
takeOrder ('pepperoni', 'hand-tossed');
takeOrder ('sausage', 'deep dish');
takeOrder ('chicken', 'thin crust');
takeOrder ('meatballs', 'hand-tossed');
takeOrder ('anchovies', 'thin crust');


//To print the Sub-Total, Tax, and Total in the format $XX.XX:
var subTotal$='Sub-Total: $';
var Tax$='Tax: $';
var Total$='Total: $';

//Math.round(...)*100)/100 is used to round the amount to two decimal places
if (getSubTotal(orderCount)%1===0) {
  console.log(subTotal$ + getSubTotal(orderCount) + '.00');
} else {
 	if (((getSubTotal(orderCount)*100)%(0.10*100))/100===0) {
    console.log(subTotal$ + (Math.round(getSubTotal(orderCount)*100)/100) + '0');
} else {
    console.log(subTotal$ + (Math.round(getSubTotal(orderCount)*100))/100);
}}

if (getTax()%1===0) {
  console.log(Tax$ + getTax() + '.00');
} else {
 	if ((Math.round(getTax()*100)%(0.10*100))/100===0) {
    console.log(Tax$ + (Math.round(getTax()*100)/100) + '0');
} else {
    console.log(Tax$ + (Math.round(getTax()*100))/100);
}}

if (getTotal()%1===0) {
  console.log(Total$ + getTotal() + '.00');
} else {
  if ((Math.round(getTotal()*100)%(0.10*100))/100===0) {
    console.log(Total$ + (Math.round(getTotal()*100)/100) + '0');
} else {
  	console.log(Total$ + (Math.round(getTotal()*100))/100);
}}


#2

Don't put space between function name and ( when calling it, looks like you're using a control statement like for/if/etc

Don't use $ in names, looks like an operator

numbers have a toFixed method that converts it to string with the specified number of digits after the decimal point total.toFixed(2)

If you need to execute some lines multiple times, make a function out of it instead of repeating the code


#3

Thanks for the tips! Here is my revised code for the same objective. It's much simpler now! :grin:

var orderCount=0;

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

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

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

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

takeOrder('bacon', 'thin crust');
takeOrder('pepperoni', 'hand-tossed');
takeOrder('sausage', 'deep dish');
takeOrder('chicken', 'thin crust');
takeOrder('meatballs', 'hand-tossed');
takeOrder('anchovies', 'thin crust');

console.log('Sub-Total: $' + getSubTotal(orderCount).toFixed(2));
console.log('Tax: $' + getTax().toFixed(2));
console.log('Total: $' + getTotal().toFixed(2));

#4

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