Question

JAVASCRIPT Create a program that uses a loop, conditional statements, and a function to complete the...

JAVASCRIPT

Create a program that uses a loop, conditional statements, and a function to complete the following:

Using a loop, ask the user to enter the item name, price, and quantity for 3 items.

Using a function, calculate the following:

  • Calculate the cost of each item and display the cost on the screen.
  • Calculate the total cost of the three items and display the total cost on the screen.

Add a shipping charge at the end of the order based on the following conditions:

  • If the grand total is greater than $500, the shipping charge is $30.
  • If the grand total is between $200 and 499, the shipping charge is $15.
  • Otherwise, the shipping charge is $10.

Display the shipping charge.

Calculate the grand total of the order which includes the total cost of the three items with the shipping charge and display it on the screen.

All the numbers should display with 2 decimal places.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello,

The following is the solution for this assignment.

I have added comments in the code for your easy reference. If you need more information or have queries, leave me a comment.

If this solution helps you with this assignment, do upvote this answer. Thank you.

//Code for this solution


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Cost Calculator</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
function main() {
totalCost = 0.0;
//Get the item details
for(var i = 0; i < 3; i++) {
var name = prompt("Enter Item Name: ");
var price = prompt("Enter the price for " + name + ": ");
var quantity = prompt("Enter the quanity for " + name + ": ");
//calculate the total item cost
totalCost += itemCost(name, price, quantity);
}
//display the total item cost - displayed to 2 decimal places
document.getElementById('totalitemcost').innerText = "Total cost: " + parseFloat(totalCost).toFixed(2);
//calculate the shipping charges
shippingCharges = 0.0;
if (totalCost > 500) {
shippingCharges = 30;
} else if(totalCost >= 200 && totalCost <= 499) {
shippingCharges = 15;
} else {
shippingCharges = 10;
}
document.getElementById('shippingcharges').innerText = "Shipping charges: " + shippingCharges;
//calculate and display the grand total - displayed to 2 decimal places
document.getElementById('grandtotal').innerText = "Grand Total: " + parseFloat(totalCost + shippingCharges).toFixed(2);
}
//function to calculate item cost and display it
function itemCost(name, price, quantity) {
var cost = price * quantity;
var text = (document.getElementById('itemscost').innerText) || "";
text += "Item: " + name +", Cost: " + cost + ". ";
document.getElementById('itemscost').innerHTML = text;
return cost;
}
</script>
</head>
<body onload="main()">
<div id="itemscost"></div>
<div id="totalitemcost"></div>
<div id="shippingcharges"></div>
<div id="grandtotal"></div>
</body>
</html>

//Output for this solution

Add a comment
Know the answer?
Add Answer to:
JAVASCRIPT Create a program that uses a loop, conditional statements, and a function to complete the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please implement the following only using Javascript. design a cash register class that can be used...

    Please implement the following only using Javascript. design a cash register class that can be used with inventory item class. The cash register class needs to: 1. ask user for item and quantity being purchased. 2. get the items cost from inventory item object 3. add 30% profit to the cost to get the items unit price 4. multiply the unit price times the quantity being purchased to get purchase subtotal 5. compute a 6% sales tax on the subtotal...

  • JAVASCRIPT FUNCTIONS Write the isPrime function and the applySipping Cost function. Then, complete the code as...

    JAVASCRIPT FUNCTIONS Write the isPrime function and the applySipping Cost function. Then, complete the code as indicated at the end in comments var cart=1 {"name":"Biscuits", "type":"regular", "category":"food", "price": 2.0}, {"name":"Monitor", "type":"prime", "category":"tech", "price": 120}, {"name":"Mouse", "type":"prime", "category":"tech", "price": 25}, {"name":"dress", "type":"regular", "category":"clothes", "price": 50}, {"name":"XL Monitor","type":"prime", "category":"tech", "price": 160}, {"name":"Cookies","type":"regular", "category":"food", "price:16). function isPrime(item) { //WRITE THIS FUNCTION // should return true if the item's type is "prime" and false otherwise let iskotPrime = item => 'isPrime(item); let applyCoupon =...

  • Write a code using loop and flag function in python jupitior notebook: Ask the user for...

    Write a code using loop and flag function in python jupitior notebook: Ask the user for the number of items they bought. Also ask the price of each item they bought and quantity purchased. Display the total amount they owe. Ask the user for a number n. Calculate 1+2+3+….+n. Ask the user for a number n. Calculate 1*2*3*…*n. Ask the user for number of rows (r) and columns (c). Print * for r rows and c columns. Ask the user...

  • *In C language *Without using Strings Write a program that uses a for loop to calculate...

    *In C language *Without using Strings Write a program that uses a for loop to calculate the average cost of a dinner for a group of 10 people. Your program will use a function that will ask the user to enter in the price of each meal and return the value entered. Your program will calculate the total due for all 10 meals, the total tip using a defined constant of 20%, and the average price of a dinner. You...

  • Javascript: Write a function that returns the total cost of any number of buckets of paint....

    Javascript: Write a function that returns the total cost of any number of buckets of paint. Ask the user how many buckets he or she is going to buy and for the cost of one bucket. Ask the user the color of the paint. Calculate and return what he or she owes. Change the color of the font to the color of the paint. Use .innerHTML to display your output. Check that the user doesn’t give you a color of...

  • I need help building code in python for this: Create a program that: Creates a sales...

    I need help building code in python for this: Create a program that: Creates a sales receipt, displays the receipt entries and totals, and saves the receipt entries to a file Prompt the user to enter the Item Name Item Quantity Item Price Display the item name, the quantity, and item price, and the extended price (Item Quantity multiplied by Item Price) after the entry is made Save the item name, quantity, item price, and extended price to a file...

  • You will create a Grade Program that will calculate students’ weighted averages. You are going to...

    You will create a Grade Program that will calculate students’ weighted averages. You are going to have the user enter lab grades, test grades, and project grades. You will then display a grade report of each individual average along with their overall average and letter grade. Ask the user to enter their lab grades. Call the averageGrade function that will allow the user to enter their grades and calculate their average. averageGrade Function: This will pass the average back to...

  • python 3 Define a function that uses two lists similar to the following to create a...

    python 3 Define a function that uses two lists similar to the following to create a dictionary: items = ['laptop', 'tablet', 'smartphone', 'reader', 'camera'] price = [749, 199, 399, 99, 1149] The dictionary should have the names of the items as keys and the prices as values. After creating the dictionary, ask the user to enter the name of an item, and display its price if the item is in the dictionary. If the item is not in the dictionary...

  • [IN JAVA] Finish the program. There are instructions in the comments. You may want to comment...

    [IN JAVA] Finish the program. There are instructions in the comments. You may want to comment out the function calls in main, and get one function to work at a time. Thanks in advance! Template import java.util.Scanner; public class Main{ public static Scanner kb = new Scanner(System.in); public static void main(String [] args) { String productName; double productPrice; int productQuantity; double shipping; int quantityDiscount; double finalTotal; productName = queryName(); productPrice = queryPrice(); productQuantity = queryQuantity(); System.out.println(); quantityDiscount = computeQuantityDiscount(productQuantity); shipping...

  • Create function files that will use a “for” loop to perform the following tasks for a...

    Create function files that will use a “for” loop to perform the following tasks for a random input array “x”: (a) Count how many elements are greater than an arbitrary integer. Display these values as well as the count using “fprintf”. (b) Calculate the arithmetic mean of array “x” without using the built-in functions “sum”, “mean”, etc. and display the result using “fprintf”.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT