babylikesburgh
08-01-2008, 04:07 AM
The point is to take in only 2 inputs, product number and quantity per DAY, and use a sentinal controlled variable to determine when all quantities of all products for each of 7 days have been entered to display the total sales. It should be simple output, one total. BUT, having to use the switch statement, I can't get any ifs, fors or anything to work inside a switch case in order to loop each product 7 times. It's aggrivating, but this is what I have:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Obtaining and Calculating User Input</title>
<script type = "text/javascript">
<!--
var product;
var product1;
var product2;
var product3;
var product4;
var product5;
var count1 = 1;
var count2 = 1;
var count3 = 1;
var count4 = 1;
var count5 = 1;
var quantity;
var quantityVal;
var quantityTotal;
var totalValue;
product = window.prompt( "Enter product number (1 through 5), -1 to quit", "0" );
while ( product != -1 )
{
switch ( product )
{
case "1":
quantity = window.prompt( "Enter amount of product number 1 sold on day " + count1 +
", press -1 when all days are entered." );
quantityVal = parseInt( quantity );
product1 = quantityVal * 2.98;
totalValue += product1;
count1 += 1;
break;
case "2":
quantity = window.prompt( "Enter amount of product number 2 sold on day " + count1 +
", press -1 when all days are entered." );
quantityVal = parseInt( quantity );
product2 = quantityVal * 4.50;
totalValue += product2;
count2 += 1;
break;
case "3":
quantity = window.prompt( "Enter amount of product number 3 sold on day " + count1 +
", press -1 when all days are entered." );
quantityVal = parseInt( quantity );
product3 = quantityVal * 9.98;
totalValue += product3;
count3 += 1;
break;
case "4":
quantity = window.prompt( "Enter amount of product number 4 sold on day " + count1 +
", press -1 when all days are entered." );
quantityVal = parseInt( quantity );
product4 = quantityVal * 4.49;
totalValue += product4;
count4 += 1;
break;
case "5":
quantity = window.prompt( "Enter amount of product number 5 sold on day " + count1 +
", press -1 when all days are entered." );
quantityVal = parseInt( quantity );
product5 = quantityVal * 6.87;
totalValue += product5;
count5 += 1;
break;
}
totalValue += totalValue;
if ( quantity = -1 )
{
product = window.prompt( "Enter product number (1 through 5), -1 to quit", "0" );
}
}
document.write( "<h3>The total retail value of all products sold last week is: $" + totalValue + "</h3>" );
// -->
</script>
</head>
<body>
<p>Click Refresh (or Reload) to run the script again</p>
</body>
</html>
Thanks for any input or at least advice on whether it's possible to nest decisions inside cases with a switch. :thumbsup:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Obtaining and Calculating User Input</title>
<script type = "text/javascript">
<!--
var product;
var product1;
var product2;
var product3;
var product4;
var product5;
var count1 = 1;
var count2 = 1;
var count3 = 1;
var count4 = 1;
var count5 = 1;
var quantity;
var quantityVal;
var quantityTotal;
var totalValue;
product = window.prompt( "Enter product number (1 through 5), -1 to quit", "0" );
while ( product != -1 )
{
switch ( product )
{
case "1":
quantity = window.prompt( "Enter amount of product number 1 sold on day " + count1 +
", press -1 when all days are entered." );
quantityVal = parseInt( quantity );
product1 = quantityVal * 2.98;
totalValue += product1;
count1 += 1;
break;
case "2":
quantity = window.prompt( "Enter amount of product number 2 sold on day " + count1 +
", press -1 when all days are entered." );
quantityVal = parseInt( quantity );
product2 = quantityVal * 4.50;
totalValue += product2;
count2 += 1;
break;
case "3":
quantity = window.prompt( "Enter amount of product number 3 sold on day " + count1 +
", press -1 when all days are entered." );
quantityVal = parseInt( quantity );
product3 = quantityVal * 9.98;
totalValue += product3;
count3 += 1;
break;
case "4":
quantity = window.prompt( "Enter amount of product number 4 sold on day " + count1 +
", press -1 when all days are entered." );
quantityVal = parseInt( quantity );
product4 = quantityVal * 4.49;
totalValue += product4;
count4 += 1;
break;
case "5":
quantity = window.prompt( "Enter amount of product number 5 sold on day " + count1 +
", press -1 when all days are entered." );
quantityVal = parseInt( quantity );
product5 = quantityVal * 6.87;
totalValue += product5;
count5 += 1;
break;
}
totalValue += totalValue;
if ( quantity = -1 )
{
product = window.prompt( "Enter product number (1 through 5), -1 to quit", "0" );
}
}
document.write( "<h3>The total retail value of all products sold last week is: $" + totalValue + "</h3>" );
// -->
</script>
</head>
<body>
<p>Click Refresh (or Reload) to run the script again</p>
</body>
</html>
Thanks for any input or at least advice on whether it's possible to nest decisions inside cases with a switch. :thumbsup: