Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The code is indented and formatted in a manner that makes it easy to read. The program is made up of small functions that are

The code is indented and formatted in a manner that makes it easy to read. The program is made up of small functions that are combined to do a larger job. Each function follows the input, process, output pattern.

This program MUST use a conditional statement, document.readyState, and a DOMContentLoaded event listener to wait for the DOM to load before running the program. You must use the waiting for the DOM code to run the program. To get the prices from the element you must use textContent, and then use the Number() method to convert the text to a number. Then you can use that number to calculate a subtotal. The correct output is NOT written to the DOM.

Here is my code, can you fix it like the comment above? My code is an error.

"use strict";

// Check to see if page is still loading.

if (document.readyState == 'loading') {

// Still loading, so add an event listener to wait for it to finish.

document.addEventListener('DOMContentLoaded', showItems)

} else {

// DOM is already ready, so run the program!

showItems();

}

// Read the prices of the 3 elements from the DOM: back-price, calc-price, and text-price.

function showItems() {

const backPrice = document.getElementsByClassName("price")[0].textContent;

const calcPrice = document.getElementsByClassName("price")[1].textContent;

const textPrice = document.getElementsByClassName("price")[2].textContent;

const prices = [backPrice, calcPrice, textPrice];

const subTotal = pricesAdd(prices);

const taxAmount = calcTax(subTotal);

const finalCostAmount = calcFinalCost(subTotal, taxAmount);

return finalCostAmount;

}

// Function to add prices

function pricesAdd(prices) {

let sum = 0;

for (let i = 0; i < prices.length; i++) {

sum += Number(prices[i]);

}

return sum;

}

// Calculate the total price of items

function calcSum() {

const prices = showItems();

const subTotal = pricesAdd(prices);

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(sum) {

let taxAmount = 0;

const rate = taxRate();

taxAmount = sum * rate;

return taxAmount;

}

// Calculate the final cost

function calcFinalCost(sum, tax) {

const finalCostAmount = sum + tax;

return finalCostAmount;

}

// Output the results

document.addEventListener('DOMContentLoaded', function () {

document.getElementById("sub-total").textContent = sum.toFixed(2);

document.getElementById("tax-amount").textContent = tax.toFixed(2);

document.getElementById("total").textContent = finalCostAmount.toFixed(2);

});

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

List the functional consequences of PTSD.

Answered: 1 week ago