Question: The goal is to: Write a webpage with an external JavaScript that uses a loop to ask the user repeatedly to enter the prices of
The goal is to:
Write a webpage with an external JavaScript that uses a loop to ask the user repeatedly to enter the prices of the items purchased or enter -1 when done.
When done, the program should display the total of the item purchased, the count of items, the average price, the highest item price, and the lowest item price.
Name the files: index.html and script.js and submit both.
One problem I run into is the let itemPurchased keeps coming back unrefined. And I cant figure out how to get the lowest number. Please help and if there is a better way to do this I am open to that as well..
Here's my code:
let averagePrice=0;
let numberofItems=0;
let itemPurchased = "";
let highestPrice=0;
let totalPrice=0;
//let lowestPrice=0;
for (let i = 1; itemPurchased!=-1; i++) {
itemPurchased = parseFloat(prompt('What is the price of the Item?'));
totalPrice += itemPurchased;
}
// Need help it says itemPurchase is undefined. But I have done this before and it went through.
if (itemPurchase > 0) {
numberofItems++;
}
averagePrice = totalPrice / numberofItems;
if (itemPurchased > highestPrice ){
highestPrice = itemPurchased;
}
// For the lowest price I need alot of Help with this.
//if (itemPurchased < lowestPrice || itemPurchased < 1 && itemPurchased > 0){
// lowestPrice = itemPurchased;
//}
console.log(`total price is ${totalPrice}`);
console.log(`number of items is ${numberofItems}`);
console.log(`average price is ${averagePrice}`);
console.log(`highest price is ${highestPrice}`);
//console.log(`lowest price is ${lowestPrice}`);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
