Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-06-2012, 10:36 PM   PM User | #1
LooseJuice
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
LooseJuice is an unknown quantity at this point
Help with a simple loop

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:

Code:
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.";
	alert(sentence);
	 
	var total = 0; 
	var price = 0;
	var discount = 0;
	
	do 
	{
	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.");
			}
	else 
	{
			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("</br>");
	document.write("With the discount, your total order cost is " + total + ".");
	
	}
	else
	{
	
	document.write("Refresh the page to reload and place a new order");
	
	}
Any help would be appreciated because I am unbelievably confused.
LooseJuice is offline   Reply With Quote
Old 11-06-2012, 11:12 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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:
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
Code:
    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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-07-2012, 07:42 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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).
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:51 PM.


Advertisement
Log in to turn off these ads.