Get Programming with JavaScript - Listing 4.12

Listing 4.12 - Using functions to add and display tax

var sale1; var sale2; var sale3; var sale; var calculateTax; var displaySale; sale1 = { price: 140, taxRate: 15 }; sale2 = { price: 40, taxRate: 10 }; sale3 = { price: 120, taxRate: 20 }; calculateTax = function () { sale.tax = sale.price * sale.taxRate / 100; sale.total = sale.price + sale.tax; }; displaySale = function () { console.log("price = $" + sale.price); console.log("tax @ " + sale.taxRate + "% = $" + sale.tax); console.log("total cost = $" + sale.total); }; sale = sale1; calculateTax(); displaySale(); sale = sale2; calculateTax(); displaySale(); sale = sale3; calculateTax(); displaySale();

Further Adventures

Listing 4.12 - Using functions to add and display tax - Tasks 1 & 2

var sale1; var sale2; var sale3; var sale4; // Declare a variable var sale; var calculateTax; var displaySale; sale1 = { price: 140, taxRate: 15 }; sale2 = { price: 40, taxRate: 10 }; sale3 = { price: 120, taxRate: 20 }; sale4 = { price: 90, taxRate: 10 }; // Create an object and assign it calculateTax = function () { sale.tax = sale.price * sale.taxRate / 100; sale.total = sale.price + sale.tax; }; displaySale = function () { console.log("price = $" + sale.price); console.log("tax @ " + sale.taxRate + "% = $" + sale.tax); console.log("total cost = $" + sale.total); }; sale = sale1; calculateTax(); displaySale(); sale = sale2; calculateTax(); displaySale(); sale = sale3; calculateTax(); displaySale(); // Calculate tax and display for sale4 sale = sale4; calculateTax(); displaySale();

Listing 4.12 - Using functions to add and display tax - Task 3

var sale1; var sale2; var sale3; ar sale4; var sale; var calculateTax; var displaySale; sale1 = { price: 140, taxRate: 15 }; sale2 = { price: 40, taxRate: 10 }; sale3 = { price: 120, taxRate: 20 }; sale4 = { price: 90, taxRate: 10 }; calculateTax = function () { sale.tax = sale.price * sale.taxRate / 100; sale.total = sale.price + sale.tax; }; displaySale = function () { calculateTax(); // Call one function from another console.log("price = $" + sale.price); console.log("tax @ " + sale.taxRate + "% = $" + sale.tax); console.log("total cost = $" + sale.total); }; // Now there's no need to call both functions for each sale sale = sale1; displaySale(); sale = sale2; displaySale(); sale = sale3; displaySale(); sale = sale4; displaySale();

This solution calls one function from another. The tax needs to be calculated before the sale can be displayed, so this is a reasonable approach.

If you wanted to keep the calculation separate from the display, you could define a third function to call the other two, as shown below.

var sale1; var sale; var calculateTax; var displaySale; var calcAndDisplay; // Declare a variable sale1 = { price: 140, taxRate: 15 }; calculateTax = function () { sale.tax = sale.price * sale.taxRate / 100; sale.total = sale.price + sale.tax; }; displaySale = function () { console.log("price = $" + sale.price); console.log("tax @ " + sale.taxRate + "% = $" + sale.tax); console.log("total cost = $" + sale.total); }; // Define a new function to call the other two calcAndDisplay = function () { calculateTax(); displaySale(); }; sale = sale1; calcAndDisplay(); // Call the new function