Question: Use my code and creating small reusable functions. The small functions will be combined to solve a larger program. Each function is like a little
Use my code and creating small reusable functions. The small functions will be combined to solve a larger program.
Each function is like a little tool that does only one job. Combined together, the little functions can do big things.
- Finally, call the function to execute it.
- The program will just template literals to construct the output to the console:
- The number of items purchased,
- The discount amount
- The subtotal,
- The sales tax amount
- The final total cost.
This is my code "use strict";
// Function to add item prices using an array and a loop function addItemPrices() { // Inputs
// Youie purchased 30 items, all the 30 items have their prices stored in the array called "prices" const prices = [6.67, 1.38, 5.77, 20.65, 4.97, 6.40, 7.34, 4.61, 9.81, 8.85, 3.66, 3.38, 6.68, 6.32, 4.55, 6.55, 70.85, 4.01, 0.81, 8.45, 2.77, 4.38, 5.25, 6.47, 40.22, 6.06, 7.98, 4.02, 2.81, 28.33];
// discount let discount;
// Total price of all the 30 items in the "prices" array before discount let totalPriceOfItemsBefDisc = 0;
// 12% sales tax in value const salesTax = 0.12;
// Total number of items purchased by youie let totalItemsPurchased;
// Amount of discount that youie receives let discountReceived;
// Subtotal cost of all the 30 items after discount let totalCostAfterDisc;
// sales tax amount youie pays let salesTaxPayable;
// Total amount Youie must pay let totalAmountPayable;
// Total number of items purchased by youie // The length property sets or returns the number of elements in an array. totalItemsPurchased = prices.length;
// Total price of all the 30 items in the "prices" array before discount using for loop for (let i = 0; i < prices.length; i++ ) { totalPriceOfItemsBefDisc += prices[i]; }
// console.log(totalPriceOfItemsBefDisc);
if (totalPriceOfItemsBefDisc > 250) { // 20% discount discount = 0.20;
// Amount of discount that youie receives discountReceived = 0.20 * totalPriceOfItemsBefDisc;
// Subtotal cost of all the 30 items after discount totalCostAfterDisc = totalPriceOfItemsBefDisc - discountReceived;
// sales tax payable after discount salesTaxPayable = 0.12 * totalCostAfterDisc;
//Total amount Youie must pay totalAmountPayable = totalCostAfterDisc + salesTaxPayable;
}
// Printing Outputs in console
// The ceil() method rounds a number UPWARDS to the nearest integer, and returns the result.
console.log('Number of items purchased' , Math.ceil(totalItemsPurchased)); console.log('Youie\'s discount amount',Math.ceil(discountReceived)); console.log('The subtotal amount', Math.ceil(totalCostAfterDisc)); console.log('The sales tax amount',salesTaxPayable.toFixed(2) ); console.log('Total price payable', totalAmountPayable.toFixed(2));
}
// calling the function addItemPrices();
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
