Answered step by step
Verified Expert Solution
Question
1 Approved Answer
/ / SO function ( assuming this is a typo, and it's meant to be some setup function ) document.addEventListener ( ' DOMContentLoaded ' ,
SO function assuming this is a typo, and it's meant to be some setup function
document.addEventListenerDOMContentLoaded function
Add an event listener to the Calculate button
document.getElementByIdcalculateBtnaddEventListenerclick calculate;
Move focus to the product cost text box
document.getElementByIdproductCostfocus;
;
function calculate
Parse the product cost
var productCost parseFloatdocumentgetElementByIdproductCostvalue;
Check if the product cost is a valid number and greater than zero
if isNaNproductCost && productCost
If valid, calculate the shipping
var totalCost calculateShippingproductCost;
You might want to display the total cost somewhere on your page
For example: document.getElementByIdtotalCosttextContent totalCost.toFixed;
else
If not valid, throw an alert
alertPlease enter a valid product cost greater than zero.;
Leave focus on the product cost
document.getElementByIdproductCostfocus;
function calculateShippingproductCost
var shippingCost;
var shippingRate;
Define shipping rates based on product cost ranges
if productCost
shippingRate ; of product cost
else if productCost
shippingRate ; of product cost
else if productCost
shippingRate ; of product cost
else
shippingRate ; of product cost
Calculate shipping cost
shippingCost productCost shippingRate;
Return the sum of product cost and shipping cost
return productCost shippingCost;
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