...

View Full Version : while loop question



Mary U
03-21-2009, 08:16 PM
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;

jmrker
03-21-2009, 09:07 PM
Try changing this part: :)


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

Mary U
03-22-2009, 01:27 PM
I'll give it a shot, thanks for your reply.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum