Question: / / 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.addEventListener('DOMContentLoaded', function(){
// Add an event listener to the Calculate button
document.getElementById('calculateBtn').addEventListener('click', calculate);
// Move focus to the product cost text box
document.getElementById('productCost').focus();
});
function calculate(){
// Parse the product cost
var productCost = parseFloat(document.getElementById('productCost').value);
// Check if the product cost is a valid number and greater than zero
if (!isNaN(productCost) && productCost >0){
// If valid, calculate the shipping
var totalCost = calculateShipping(productCost);
// You might want to display the total cost somewhere on your page
// For example: document.getElementById('totalCost').textContent = totalCost.toFixed(2);
} else {
// If not valid, throw an alert
alert('Please enter a valid product cost greater than zero.');
// Leave focus on the product cost
document.getElementById('productCost').focus();
}
}
function calculateShipping(productCost){
var shippingCost;
var shippingRate;
// Define shipping rates based on product cost ranges
if (productCost <=50){
shippingRate =0.1; //10% of product cost
} else if (productCost <=100){
shippingRate =0.07; //7% of product cost
} else if (productCost <=200){
shippingRate =0.05; //5% of product cost
} else {
shippingRate =0.02; //2% 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!