Hello I am trying to display the total cost after the user enters a product ID, controlled by a switch statement. However the total variable ends up being the product ID, not the price. I want the total to be the sums of the prices.

<script type ="text/javascript">
// Switch statement & variables with their proper values, declare the product and total too 
var product;
var price;
var total = 0;
switch(product)
{
  case 1:
    price = 2.98;
    break;    
  case 2:
    price = 4.50;
    break;
  case 3:
    price = 9.98;
    break;  
  case 4:
    price = 4.49;
    break;  
  case 5:
    price = 6.87;
    break;  
  default:
    price = undefined;
}

document.write("Welcome, please enter a product number. Press '0' to terminate the buying sequence.");
document.write("<br />");
document.write("Enter the number (1 - 5) of the product you would like.");
document.write("<br />");

// Until the user enters "end" keep asking what product they want
while(product != 0)
{
    product = prompt("Please enter which product you would like: ");
    total = total + price;
}

document.write("The total cost is: $" + total); 
</script>

<h3>P.333 10.13</h3>
<script type ="text/javascript">

</script>
// Until the user enters "end" keep asking what product they want
while(product != 0)
{
    product = prompt("Please enter which product you would like: ");
    total = total + price;
}

In the while loop, I don't see the program enter the switch block of code, so the "price" variable is never re-assigned. The code just sees "price" (= 0) being added to "total".

The way I understand the switch statement is that when the variable in this case "product" is being used, as it is in the while loop, I enter in the prompt "1", and then price is equal to what the switch statement says in case 1.

I'm pretty sure there is something else to this, but I haven't used a switch loop in ages, and I did it in java when I used it.

The problem is the order you are executing the statements in.

JavaScript executes the statements in the order you place them in the program. You have the switch statement before the statements that get the info to work it.

// Until the user enters "end" keep asking what product they want
while(product != 0)
{
    product = prompt("Please enter which product you would like: ");
    [B]// you need to put the entire switch structure HERE, or it won't work.[/B]
    total = total + price;
}

Ok, I changed the switch statement so it's right where you said to put it, but I'm still getting an answer of NaN (Not a number). I'll put in simple values like 2,3,0. I'm not sure why I get the NaN, here's what my code looks like now:

<script type ="text/javascript">
// Variables with their proper values, declare the product and total too 
var product;
var price;
var total = 0;


document.write("Welcome, please enter a product number. Press '0' to terminate the buying sequence.");
document.write("<br />");
document.write("Enter the number (1 - 5) of the product you would like.");
document.write("<br />");

// Until the user enters "0" keep asking what product they want
while(product != 0)
{
    product = prompt("Please enter which product you would like: ");
    // Switch the price of the product, based on what the user enters
    switch(product)
    {
      case 1:
      price = 2.98;
        break;    
      case 2:
      price = 4.50;
        break;
      case 3:
      price = 9.98;
        break;  
      case 4:
      price = 4.49;
        break;  
      case 5:
      price = 6.87;
        break;  
      default:
      price = undefined;
    }
    total = total + price;
}

document.write("The total cost is: $" + total); 
</script>

The prompt function returns a string. The switch construct and so also your calculations requires product be a numeric value. Convert the user input returned to a number before using it.

do
{
   product = Number(prompt("Enter something"));
   if(isNaN(product))
      product = -1;
   switch(product)
   {
      case 1: price = 1.0; break;
      /* and so on */
      default: price = 0;
   }
   total += price;
} while(product != 0);

Don't use document.write(). It's far from flexible. There are better ways of dynamically or programatically appending content to your document, known as DOM manipulation.

Thanks that worked!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.