Page 1 of 1

Please Help with this. There are two answers already about it but they are both wrong thanks in advance. Use your code e

Posted: Fri Jul 01, 2022 5:34 am
by answerhappygod
Please Help with this. There are two answers already about itbut they are both wrong thanks in advance.
Use your code editor to openthe project04-01_txt.html and project04-01_txt.js filesfrom the js04 ► project01 folder. Enter your name and the date inthe comment section of each file and save themas project04-01.html and project04-01.js,respectively.
Go to the project04-01.html file inyour code editor and in the head section adda script element to load the project04-01.js file,deferring the loading of the program until the web page finishesloading. Study the contents of the HTML to become family with theelements and the ids associated with each element. Save yourchanges to the file.
Go to the project04-01.js file inyour code editor. At the top of the file above the comment section,insert a statement that indicates this program adheres to a strictinterpretation of JavaScript syntax.
There are two logic errors in setting up the global constants.Locate and fix those errors.
Go to the calcTotal() function. To guard against usersentering a zero or negative value for the estimated weight, replacethe statement totalCost += wgtBox.value * COST_PER_LB; witha try catch statement that does the following:
Tests whether wgtBox.value is not greater than 0 byusing the expression !(wgtBox.value > 0). If thatexpression is true, throw an exception with the errormessage “!! Enter a positive weight”
If no exception is thrown then run the command totalCost +=wgtBox.value * COST_PER_LB;.
If an exception is caught, set the valueof msgBox.innerHTML to the error message you defined forthe thrown exception.
Repeat Step 5, for the estimated distance costusing distBox.value. Throw the error message “!!Enter a positive mileage” for a caught exception.
Save your changes to the file.
Open the project04-01.html file inyour web browser. Open the debugger and confirm that there are nosyntax errors. If there are, fix your code in your code editor.
Test your code by entering 0 for theestimated weight and 1200 for themileage. Confirm that the error message “!! Enter a positiveweight” appears next to the total box.
Change the weight to 2500 and entera 0 for the estimated mileage. Confirmthat the error message “!! Enter a positive mileage” appears nextto the total box.
Enter a mileage of 1200 miles andclick the Setup and Installation box. Confirm that the estimatedmoving cost is calculated to be $2650.
<html><head> <!-- JavaScript 7th Edition Chapter 4 Hands-on Project 4-1
Author: Date:
Filename: project04-01.html --> <meta charset="utf-8" /> <meta name="viewport"content="width=device-width,initial-scale=1.0"> <title>Hands-on Project 4-1</title> <link rel="stylesheet" href="styles.css"/> <script src="project04-01.js"defer></script></head>
<body> <header> <h1> Hands-on Project 4-1 </h1> </header>
<article> <h2>Moving Estimate</h2> <form> <labelfor="wgtBox">Estimated weight @ $0.50 perpound</label> <input id="wgtBox" /><span>lbs.</span> <labelfor="distBox">Estimated distance @ $0.75 permile</label> <input id="distBox" /><span> miles</span> <label for="setupBox"id="setupLabel">Setup and Installation (add$500)</label> <input type="checkbox"id="setupBox" /> <divid="border"></div> <label>Estimated MovingCost</label> <divid="totalBox"></div> <spanid="msgBox"></span> </form> </article></body></html>
/* JavaScript 7th Edition Chapter 4 Project 04-01
Application to calculate total movingcost Author: Date:
Filename: project04-01.js*/
// Global Constantsconst COST_PER_LB = 50;const COST_PER_MILE = 75;const SETUP_COST = 500;
// Global Variableslet wgtBox = document.getElementById("wgtBox");let distBox = document.getElementById("distBox");let msgBox = document.getElementById("msgBox");
// Event Handlersdocument.getElementById("wgtBox").onchange = calcTotal;document.getElementById("distBox").onchange = calcTotal;document.getElementById("setupBox").onclick = calcTotal;
// Function to calculate an estimate of the total movingcostfunction calcTotal() { let totalCost = 0; // Set theinitial estimate to $0 msgBox.innerHTML = ""; // Erase any warnings inthe message box
totalCost += wgtBox.value * COST_PER_LB;
totalCost += distBox.value * COST_PER_MILE; if (document.getElementById("setupBox").checked){ totalCost += SETUP_COST } // Display the moving cost estimate in the totalBox,formatted as currency document.getElementById("totalBox").innerHTML =formatCurrency(totalCost);}
// Function to display a numeric value as a text string inthe format $##.## function formatCurrency(value) { return "$" + value.toFixed(2); }