•
•
•
•
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 427,187 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 2,197 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: 2713 | Replies: 6
![]() |
•
•
Join Date: Feb 2007
Posts: 56
Reputation:
Rep Power: 2
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: 73
Reputation:
Rep Power: 3
Solved Threads: 0
•
•
•
•
// 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: 56
Reputation:
Rep Power: 2
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 3:16 pm.
Daylight-saving time uses more gasoline
•
•
Join Date: Feb 2007
Posts: 56
Reputation:
Rep Power: 2
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.
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.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Using a switch statement with nested if/else statements (C++)
- switch statement on String in Java (Java)
- 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



Linear Mode