Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

On line 30, instead of using index numbers to get each item in the array to calculate the sum, just add the prices using a

On line 30, instead of using index numbers to get each item in the array to calculate the sum, just add the prices using a function like the pricesAdd example on Functions: Sample Program page from the Week 4 materials. That function will work with any number of prices - instead of only working with 3 prices.

On lines 54-56 you must break up into multiple lines. On future assignments you MUST read the textContent of the element on a different line than the line that used getElementById to read the element. That will let you add some additional code to provide some error testing.

On lines 59 and 60 there are 2 variables that are defined in global scope. This is not okay. All variables must be stored inside of functions.

Can you help me fix this code like the comments above? The code is running fine, so don't change it, just fix it like the comments. 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(sum) {

let taxAmount = 0;

const rate = taxRate();

taxAmount = sum * rate;

return taxAmount;

}

// Calculate the final cost

function calcFinalCost(sum, tax) {

let finalCostAmount = 0;

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);

}

const sum = calcSum();

const tax = calcTax(sum);

calcFinalCost(sum, tax);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

More Books

Students also viewed these Databases questions