2 Replies - 604 Views - Last Post: 23 October 2009 - 09:40 PM

#1 jimmynewtr0nIRL  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-October 09

document.write printing out

Post icon  Posted 23 October 2009 - 09:19 PM

 <html>
<head>
	<title> Javascript Assignment 2: Matt Ciullo </title>
	<!-- Matt Ciullo Annath JS  - Part 2 - Implementation October 16, 2009 -->

</head>
<body bgcolor="99FFFF" >

			<h1 align="center"> The Office- eCommerce </h1>

			<td> <table width="100%" height="70%" border="3" align="center">
				<tr height="30"> <td align="center" valign="top"> <font size="5"> Product name </font> </td> <td valign="top" align="center"> <font size="5"> Price </font> </td>
			 	<td align="center" valign="top"> <font size="5"> Discount </font> </td> </tr>
				<tr> <td> Printer </td> <td> $79.99 </td> <td> .1 </td> </tr>
				<tr> <td> Chair </td> <td> $189.99 </td> <td> .2 </td>
				<tr> <td> Monitor</td> <td> $169.99 </td> <td> .5 </td> </tr>
				<tr> <td> Pens </td> <td> $15.99 </td> <td> .05 </td> </tr>
				
	</table>
			<script type="text/javascript">
		
		var name;
		var product;
		var a;
		var cost = 0;
		var pprice = 0;
		var discount = 0;
		var quantity = 0;
		var totalcost = 0;
		var finalcost = 0;
		
		name = prompt("Welcome to The Office Please enter your name." , "");
		alert("Hello " + name + " Please look through our available products and services before placing your order." , "");
		product = prompt("What product would you like to order today?", "");
		pprice = 1*prompt("What's the price of the product you just ordered ?", "");
		quantity = 1*prompt("Finally, how many " + product + "s would you like?", "");


	
			a = confirm(name + "You ordered " + quantity  + product + "s, Is this ok?");

				if (a == false)
				{
				alert("Sorry, your item has not been found, please refresh and place another order.");
				}
						
				totalcost = (quantity*pprice);
				if (product == "printer")
				{
					alert("You receive 10% discount!!!");
					totalcost = (quantity*pprice);
					cost = 0.9*(totalcost);
					discount = .1*(totalcost);
					
				}
				else if (product == "chair")
				{
					alert("You receive 20% discount!!!");
					cost = 0.8*(totalcost);
					discount = .2*(totalcost);
					
					
				}
				else if (product == "monitor")
				{
					alert("You receive 50% discount!!!");
					cost = 0.5*(totalcost);
					discount = .5*(totalcost);
					
				}
				else if (product == "pens")
				{
					alert("You receive 5% discount!!!");
					cost = 0.95*(totalcost);
					discount = .05*(totalcost);

				}
				else
					document.write(" Sorry," + name + "You entered an invalid product. Refresh the page to reload and place the order again.");
				
				finalcost=(totalcost-discount);
		document.write("Thank you for placing an order with us, " + name + ".<br> " );
		document.write("The cost of buying " + quantity + " of " + product + "s is $ " + totalcost + ".<br> " );
		document.write("The discount for this purchase is $ " + discount + ".<br> ");
		document.write("With the discount, your total order cost is $" + finalcost + ".<br> "  );
					

	</script>	
</body>
</html>


So when I get to the confirm statement, even if I hit cancel when it asks if you're sure you want this, it still prints out the discount, and all of the document.writes even though I do not want it to. If anyone could help it'd be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: document.write printing out

#2 jimmynewtr0nIRL  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-October 09

Re: document.write printing out

Posted 23 October 2009 - 09:32 PM

Could still use some help, I know it's a really simple step, but I just cant for the life of my figure it out, and I'm desperate.
Was This Post Helpful? 0
  • +
  • -

#3 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: document.write printing out

Posted 23 October 2009 - 09:40 PM

finalcost=(totalcost-discount);
        	document.write("Thank you for placing an order with us, " + name + ".<br> " );
        	document.write("The cost of buying " + quantity + " of " + product + "s is $ " + totalcost + ".<br> " );
        	document.write("The discount for this purchase is $ " + discount + ".<br> ");
        	document.write("With the discount, your total order cost is $" + finalcost + ".<br> "  );
                


these are a bunch of statements, and since they're just out in the open (not affected by any conditional statements) they are called no mater what. Put them in side a function, and reorganize your code a little. Just a simple logic error :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1