View Full Version : Help with a simple loop

11-06-2012, 11:36 PM
I'm supposed to make a simple order calculator using a loop. I need to have a web page which asks a user what they want to order, when they type in the name of the product and the quantity, the web page will look up and calculate the price. The page then asks them if they want to order more and if they click okay, another box comes up again asking them what they want to order and how much they want. The whole time, a running total is supposed to be kept so that when they finally click that they do not want to order anymore, the program terminates and shows them their total.

This is the code I have so far:

var name, product, quantity, confirm, total, price, discount;

var name = prompt("Welcome to Ice Cream Shop. What is your name?", "Enter name");
var sentence = "Hello " + name + " please look through our available products and services before placing your order.";

var total = 0;
var price = 0;
var discount = 0;

var product = prompt("What would you like to order?","");
var quantity = prompt ("What would you like to order?","");
total = total + price;
var confirm = continue("Do you want to order anything else?");

while (confirm==true);

if(product === "ice cream cake") {
price = 20 - discount;
discount = (.15 * price);
} else if (product === "ice cream cone") {
price = 3 - discount;
discount = (.01 * price);
} else if (product === "small ice cream sundae") {
price = 5 - discount;
discount = (.05 * price);
} else if (product === "large ice cream sundae") {
price = 6 - discount;
discount = (.05 * price);
} else alert("Sorry, " + name + ". You entered an invalid product. Refresh the page to reload and place the order again.");
alert("Refresh the page to reload and place a new order");

if (product == "ice cream cake" || product == "ice cream cone" || product == "small ice cream sundae" || product == "large ice cream sundae") {

document.write("Thank you for placing an order with us, " + name + ".");
document.write("With the discount, your total order cost is " + total + ".");


document.write("Refresh the page to reload and place a new order");


Any help would be appreciated because I am unbelievably confused.

Old Pedant
11-07-2012, 12:12 AM
If it is supposed to be a web page, then why are you using prompt() instead of and HTML <form> and form fields? No real web page would ever use prompt() as you are doing.

Anyway there are several things in your code that don't make sense.

First of all, you keep asking the user for a product inside that loop.
But you store *ALL* the answers into the *ONE* variable named product.
So the second time through the loop, you erase the answer you got in the first time through the loop.
And so on.
When the loop is finally finished the ONLY product you will have is the LAST one the user entered.

Also inside the loop, you keep doing total = total + price. But price is zero when you start the loop and is NEVER CHANGED inside the loop, so you will just keep adding zero to the total.

Then you have all this code:

if (product === "ice cream cone")
price = 3 - discount;
discount = (.01 * price);

But discount is set to zero at the top of your code and is not changed before you get to this code. So you are, really, doing

price = 3 - 0;

What's the point?


Try this: Pretend YOU are the computer. Or maybe pretend you are in-person asking the customer those questions.

What steps would YOU take and WHEN would you take them, in order to get a correct answer?

WRITE DOWN the steps you would use.

Then get some friend to play the game with you: You ask the questions, you do EXACTLY what your written down steps tell you to do. Does it make sense? Do you get the right answer at the end?
If not, edit those WRITTEN steps and try again, until it works.

*NOW* convert those steps to a program.

Philip M
11-07-2012, 08:42 AM
I have to say that asking the user to enter the name of a product via a prompt box is completely impractical. The slightest mis-spelling or incorrect case will cause the script to fail to recognise it - e.g. Large ice cream Sundae or small icecream sundae or ice cream Cone. The chance of a successful order is rather low.

It would make sense for the user to select the product and the desired quantity using drop-downs (select lists).