Question
Solve this Problem Write a JavaScript program that solves the following problem: Youie buys 30 different items at the campus bookstore. The bookstore gives a
Solve this Problem
Write a JavaScript program that solves the following problem:
Youie buys 30 different items at the campus bookstore.
The bookstore gives a 20% discount if the prices of the items total more than $250.
The bookstore charges a 12% sales tax.
How many items does Youie purchase?
What is the amount of the discount that Youie receives?
What is the subtotal cost for the 30 items after the discount has been applied?
What is the sales tax amount?
What is the total amount that Youie must pay?
Set Up This Assignment
You will create new folders and files for this assignment.
Follow these steps to create the folders and files for this assignment:
Create a "week4" folder.
Create an "index.html" file in the "week4" folder. This file will be the main page for this assignment.
Create a "week4/js" folder.
Create a "scripts.js" file in the "week4/js" folder. This file will contain all the JavaScript for this assignment.
Use this array of data for the prices of the 30 items purchased:
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] ;
Instructions
The program must meet these criteria:
Must follow the standards followed in earlier programs: Has meaningful comments. Uses strict mode to enforce good coding practices. Variables are initialized at the beginning. Uses "const" and "let" correctly. It does not use "var" to initialize variables. All code is valid and easy to read.
The HTML file for this assignment must be located at "public_html/csci212/week4/index.html" and use the HTML provided above - which is based on the HTML5 Sample Template.
The JavaScript file for this assignment must be located at "public_html/csci212/week4/js/scripts.js".
Follow the advice on the "Functions: Do One Thing (DOT)" page to break your code up into small reusable functions. At a minimum, you should create a different function for each of these jobs. I've made up some names, but you don't have to use these names:
processPurchase(prices) - This is the main program that calls all the other small functions to solve this program.
addPrices(prices) - accepts an array as the parameter and returns a number.
applyDiscount(subTotal) - accepts a number, returns a number.
discountRate() - returns the current discount rate as a decimal number.
calcSalesTax(subTotal) - accepts a number, returns a number. Calls salesTaxRate() to get the current tax rate.
salesTaxRate() - returns the current tax rate as a decimal number.
Finally, call the main function to execute it.
The program must just template literals to construct the output to the console:
The number of items purchased,
The discount amount
The subtotal after discount,
The sales tax amount
The final total cost.
I already code it but there is one error :
Uncaught ReferenceError: prices is not defined at scripts.js:58:17
Here is my code:
"use strict";
function processPurchase(prices) {
// Main program
let numItems = prices.length;
let subTotal = addPrices(prices);
let discountAmount = applyDiscount(subTotal);
let salesTax = calcSalesTax(subTotal);
let totalCost = subTotal + salesTax - discountAmount;
console.log(`Number of items purchased: ${numItems} Discount Amount: ${discountAmount} Subtotal: ${subTotal} Sales Tax: ${salesTax} Final Total Cost: ${totalCost}`);
};
// Break down function
// Function to add prices
function addPrices(prices) {
let subTotal = 0;
for (let i = 0; i < prices.length; i++) {
subTotal += prices[i];
}
return subTotal;
}
// Function to apply discount
function calcDiscount(subTotal) { // Accepts a number, returns a number.
let discount = subTotal * discountRate;
return discount;
};
// Function to return discount rate
function discountRate() { // Returns the current discount rate as a decimal number.
const rate = 0.20; // The bookstore gives a 20% discount.
return rate;
};
// Function to calculate sales tax
function calcSalesTax(subTotal) { // Accepts a number, returns a number. Calls salesTaxRate() to get the current tax rate.
let salesTax = subTotal * salesTaxRate;
return salesTax;
};
// Function to return sales tax rate
function salesTaxRate() { // Returns the current tax rate as a decimal number.
const rate = 0.12; // The bookstore charges a 12% sales tax.
return rate;
};
// Call the main function
processPurchase(prices);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started