Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-21-2009, 08:16 PM   PM User | #1
Mary U
New Coder

 
Join Date: Feb 2009
Posts: 23
Thanks: 4
Thanked 0 Times in 0 Posts
Mary U is an unknown quantity at this point
while loop question

Evening all:

I'm working on a for loop and was hoping for some help. My husband and I collect records, 45's that is. I'm trying to develop an interface that would allow input and then return a dollar value.

For example, I would like a prompt to ask for the number of records, then loop through alert boxes asking for dollar value for each record, then return a total dollar value.

I'm missing something, and I'm not sure what, I've been through it many times and somehow keep getting errors, or no output. Could someone please look at this and let me know what I am missing?

function calculate(){
var recnumber = parseInt(prompt("How many records were purchased?","2")); //prompt for number of records (45s)
var rectotal = parseFloat(0); // declare and initialize running total
var counter = 0; //counter

while (counter <= recnumber)
{
var indrec = parseFloat(prompt("What is the dollar value of the record?","12.00"); //prompt for dollar value of a single record
rectotal = (rectotal + indrec); //add indvidual record value to running total
}
return rectotal;
Mary U is offline   Reply With Quote
Old 03-21-2009, 09:07 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
Lightbulb

Try changing this part:
Code:
do {
  var indrec = parseFloat(prompt("What is the dollar value of the record?","12.00"); //prompt for dollar value of a single record
  rectotal = (rectotal + indrec); //add indvidual record value to running total
}
while (counter <= recnumber);
return rectotal;
Above version causes execution of script at least once.

Other versions of while (condition) { doSomething }
if not written correctly, would cause script to be jumped over and not executed.

See: http://www.w3schools.com/js/js_looping.asp
jmrker is offline   Reply With Quote
Old 03-22-2009, 01:27 PM   PM User | #3
Mary U
New Coder

 
Join Date: Feb 2009
Posts: 23
Thanks: 4
Thanked 0 Times in 0 Posts
Mary U is an unknown quantity at this point
I'll give it a shot, thanks for your reply.
Mary U is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:25 AM.


Advertisement
Log in to turn off these ads.