...

View Full Version : Can the switch statement not have nested decisions?



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:

Philip M
08-01-2008, 08:38 AM
You are going about it in a terribly long-winded way.

Try this:-


<script type = "text/javascript">

var x = 0;
var totalval = 0;
for (var prods = 1; prods <= 5; prods ++) {
var prodval = 0
for (var days = 1; days <= 7; days ++) {
x = parseInt(window.prompt ("Please enter the quantity of product " + prods + " sold on day " + days, ""));
if (isNaN(x) || x < 0) {
x = 0;
days --;
alert ("Invalid input! Please re-enter.")
}

if (prods == 1) {up = 2.98}
if (prods == 2) {up = 4.50}
if (prods == 3) {up = 9.98}
if (prods == 4) {up = 4.49}
if (prods == 5) {up = 6.87}
prodval = prodval + (x * up);
}
totalval = totalval + prodval;
}
alert ("The total value of products sold is $" + totalval.toFixed(2));

</script>


Q. If you were to spell out numbers, how far would you have to go until you would find the letter "A"?
A. One thousand

babylikesburgh
08-01-2008, 04:00 PM
Well, a switch statement is required, which is exactly what is tripping me up on this whole thing. I'm not accustomed to using it, and I've scoped out everything I can about it in my book and online, and still nothing. I feel like I'm making it much harder than it is, but heck, that fact isn't helping me very much ^_^

Any advice on the switch statement?

Philip M
08-01-2008, 04:14 PM
Your problem is that the case values of the switch statement are not the same data type.

For the case to be a match, the data types must match. Think for it as switch statements including === (strict equal) instead of == (equal) for case comparisons.


y = 5; // y is a number
switch (y) {
case '5': // '5' is a string
alert ("Hello World!"); // this alert will not show since the data types don't match
}
In the above case, there is no match as 5 and 5′ are not the same datatype.

Change:

switch ( product ) {
case "1":

to

switch ( product ) {
case 1:

babylikesburgh
08-01-2008, 04:22 PM
Well, I tried that but it's still not even prompting for the first number. This is what my actual problem is as far as I can tell.. It takes in the quantity for the day for each item. But, i need the total of each item for 7 days, not just the one day that is used for input. So somehow, I have to cycle each case 7 times, but I cannot figure out how to do that, and I've searched high and low about how to do it. It even asks that we use a sentinal, which I did for the procudt number prompt, but the quantity prompt input is what I need cycled. I'm lost!!

Philip M
08-01-2008, 05:19 PM
product = parseInt(window.prompt( "Enter product number (1 through 5), -1 to quit", "0" ));



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum