| | |
JavaScript Switch Statement Problems
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Feb 2007
Posts: 72
Reputation:
Solved Threads: 0
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.
javascript Syntax (Toggle Plain Text)
<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>
My Website <-- check out my site!
•
•
Join Date: Jul 2006
Posts: 88
Reputation:
Solved Threads: 2
•
•
•
•
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
// 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".
•
•
Join Date: Feb 2007
Posts: 72
Reputation:
Solved Threads: 0
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.
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!
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.
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
•
•
Join Date: Feb 2007
Posts: 72
Reputation:
Solved Threads: 0
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:
javascript Syntax (Toggle Plain Text)
<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>
My Website <-- check out my site!
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.
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- switch statement on String in Java (Java)
- Using a switch statement with nested if/else statements (C++)
- Switch Statement, Fall-Through. (C)
- Problems with switch statement (C++)
- Problems with switch statement (C++)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: JavaScript and Browsers IE
- Next Thread: JavaScript Issues
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxhelp animate array automatically beta box bug calendar cart checkbox child class codes column cookies createrange() css cursor date debugger decimal design dom download dropdown editor element enter error explorer file firefox focus forms frameworks getselection google gwt html htmlform ie8 iframe images index internet java javascript jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math menu microsoft mimic mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent php player post problem programming progressbar prototype redirect regex runtime safari scale scriptlets search select session shopping size sql text textarea toggle variables w3c web website window windowofwords windowsxp wysiwyg \n






