Question
When you run a function save the result into a variable, then pass that variable into the other functions as input parameters. More like this:
When you run a function save the result into a variable, then pass that variable into the other functions as input parameters. More like this: const sum = calcSum(); const tax = calcTax(sum); Run the calcTax and calcSum functions only once.
The run fine, but I need to fix it like the comments above. Here is my code:
"use strict";
// Read the prices of the 3 elements from the DOM: back-price, calc-price, and text-price.
function showItems() {
const backPrice = document.getElementById("back-price");
const calcPrice = document.getElementById("calc-price");
const textPrice = document.getElementById("text-price");
// Read the text contents from the 3 DOM elements.
const backpackPrice = backPrice.textContent;
const calculatorPrice = calcPrice.textContent;
const textbookPrice = textPrice.textContent;
// Store the prices in an array
const pricesArray = [backpackPrice, calculatorPrice, textbookPrice];
return pricesArray;
}
// Calculate the total price of items
function calcSum() {
let subTotal = 0;
const prices = showItems();
subTotal = Number(prices[0]) + Number(prices[1]) + Number(prices[2]);
return subTotal;
}
// Declare the tax rate
function taxRate() {
const taxRate = 0.13; // The bookstore charges 13% sales tax.
return taxRate;
}
// Calculate the sales tax
function calcTax() {
let taxAmount = 0;
const sum = calcSum();
const rate = taxRate();
taxAmount = sum * rate;
return taxAmount;
}
// Calculate the final cost
function calcFinalCost() {
let finalCostAmount = 0;
const sum = calcSum();
const tax = calcTax();
finalCostAmount = sum + tax;
// Output the results
document.getElementById("sub-total").textContent = sum.toFixed(2);
document.getElementById("tax-amount").textContent = tax.toFixed(2);
document.getElementById("total").textContent = finalCostAmount.toFixed(2);
}
calcFinalCost();
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