User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 456,572 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,636 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2931 | Replies: 6
Reply
Join Date: Feb 2007
Posts: 60
Reputation: adaykin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Question JavaScript Switch Statement Problems

  #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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 78
Reputation: DavidB is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
DavidB DavidB is offline Offline
Junior Poster in Training

Re: JavaScript Switch Statement Problems

  #2  
Oct 28th, 2007
Originally Posted by adaykin View Post
// 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".
Reply With Quote  
Join Date: Feb 2007
Posts: 60
Reputation: adaykin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: JavaScript Switch Statement Problems

  #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  
Join Date: Jan 2007
Posts: 2,604
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 119
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: JavaScript Switch Statement Problems

  #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  
Join Date: Feb 2007
Posts: 60
Reputation: adaykin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: JavaScript Switch Statement Problems

  #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  
Join Date: Jun 2006
Location: India
Posts: 7,012
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: JavaScript Switch Statement Problems

  #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.

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.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Feb 2007
Posts: 60
Reputation: adaykin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: JavaScript Switch Statement Problems

  #7  
Oct 29th, 2007
Thanks that worked!
My Website <-- check out my site!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 6:03 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC