JavaScript Switch Statement Problems

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

JavaScript Switch Statement Problems

 
0
  #1
Oct 27th, 2007
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.
  1. <script type ="text/javascript">
  2. // Switch statement & variables with their proper values, declare the product and total too
  3. var product;
  4. var price;
  5. var total = 0;
  6. switch(product)
  7. {
  8. case 1:
  9. price = 2.98;
  10. break;
  11. case 2:
  12. price = 4.50;
  13. break;
  14. case 3:
  15. price = 9.98;
  16. break;
  17. case 4:
  18. price = 4.49;
  19. break;
  20. case 5:
  21. price = 6.87;
  22. break;
  23. default:
  24. price = undefined;
  25. }
  26.  
  27. document.write("Welcome, please enter a product number. Press '0' to terminate the buying sequence.");
  28. document.write("<br />");
  29. document.write("Enter the number (1 - 5) of the product you would like.");
  30. document.write("<br />");
  31.  
  32. // Until the user enters "end" keep asking what product they want
  33. while(product != 0)
  34. {
  35. product = prompt("Please enter which product you would like: ");
  36. total = total + price;
  37. }
  38.  
  39. document.write("The total cost is: $" + total);
  40. </script>
  41.  
  42. <h3>P.333 10.13</h3>
  43. <script type ="text/javascript">
  44.  
  45. </script>
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 88
Reputation: DavidB is an unknown quantity at this point 
Solved Threads: 2
DavidB DavidB is offline Offline
Junior Poster in Training

Re: JavaScript Switch Statement Problems

 
0
  #2
Oct 28th, 2007
Originally Posted by adaykin View Post
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // Until the user enters "end" keep asking what product they want
  2. while(product != 0)
  3. {
  4. product = prompt("Please enter which product you would like: ");
  5. total = total + price;
  6. }

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".
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: JavaScript Switch Statement Problems

 
0
  #3
Oct 28th, 2007
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.
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: JavaScript Switch Statement Problems

 
0
  #4
Oct 28th, 2007
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: ");
    // you need to put the entire switch structure HERE, or it won't work.
    total = total + price;
}
Last edited by MidiMagic; Oct 28th, 2007 at 4:16 pm.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: JavaScript Switch Statement Problems

 
0
  #5
Oct 29th, 2007
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:

  1. <script type ="text/javascript">
  2. // Variables with their proper values, declare the product and total too
  3. var product;
  4. var price;
  5. var total = 0;
  6.  
  7.  
  8. document.write("Welcome, please enter a product number. Press '0' to terminate the buying sequence.");
  9. document.write("<br />");
  10. document.write("Enter the number (1 - 5) of the product you would like.");
  11. document.write("<br />");
  12.  
  13. // Until the user enters "0" keep asking what product they want
  14. while(product != 0)
  15. {
  16. product = prompt("Please enter which product you would like: ");
  17. // Switch the price of the product, based on what the user enters
  18. switch(product)
  19. {
  20. case 1:
  21. price = 2.98;
  22. break;
  23. case 2:
  24. price = 4.50;
  25. break;
  26. case 3:
  27. price = 9.98;
  28. break;
  29. case 4:
  30. price = 4.49;
  31. break;
  32. case 5:
  33. price = 6.87;
  34. break;
  35. default:
  36. price = undefined;
  37. }
  38. total = total + price;
  39. }
  40.  
  41. document.write("The total cost is: $" + total);
  42. </script>
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,617
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: JavaScript Switch Statement Problems

 
1
  #6
Oct 29th, 2007
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.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. do
  2. {
  3. product = Number(prompt("Enter something"));
  4. if(isNaN(product))
  5. product = -1;
  6. switch(product)
  7. {
  8. case 1: price = 1.0; break;
  9. /* and so on */
  10. default: price = 0;
  11. }
  12. total += price;
  13. } 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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: JavaScript Switch Statement Problems

 
0
  #7
Oct 29th, 2007
Thanks that worked!
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC