var subtotal = 24.95; // A fake subtotal value to work with function computeHandler() { var stateSelector, shippingSelector, discountText, resultText; var total, formatter, dollarAmount; // Gather the page elements we need to interact with stateSelector = document.getElementById('state'); shippingSelector = document.getElementById('shipping'); discountText = document.getElementById('discount'); resultText = document.getElementById('result'); // Start the computation if(discountText.value == 'SWEET10') { // 10% discount for entering a discount code total = 0.90 * subtotal; } else { // No discount to the subtotal total = subtotal; } if(shippingSelector.value == 'ground') { // Ground shipping is 8.95 total = total + 8.95; } else if(shippingSelector.value == 'express') { // Express shipping is 14.95 total = total + 14.95; } if(stateSelector.value == 'WI') { // Wisconsin customers get assessed a 5% sales tax total = 1.05*total; } // Create a number formatter. formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, }); // Present the result dollarAmount = formatter.format(total); resultText.textContent = 'Your total is ' + dollarAmount + '.'; } function setup() { var subtotalSpan, computeButton; subtotalSpan = document.getElementById('subtotal'); subtotalSpan.textContent = '$' + subtotal; computeButton = document.getElementById('compute'); computeButton.addEventListener('click',computeHandler,false); } window.addEventListener('load',setup,false);