Answered step by step
Verified Expert Solution
Link Copied!

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.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

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

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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

what is a peer Group? Importance?

Answered: 1 week ago

Question

Conduct a needs assessment. page 269

Answered: 1 week ago