Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

odd array loop behaviour

Status
Not open for further replies.

jez

Programmer
Apr 24, 2001
370
VN
Hi everyone,

I have an odd situation where i am looping through an array, nice basic stuff. I can show the loop works through alerts.
What i want to do in the loop is some string manipulation to sum the values and generate a total.

My array is values like 2,000.00 and 14.65 ... cash values.
If in my array i pass out the value to a function i have called "unformat" the return value is what i want but the loop ends...
So, if there there is 2 lines i.e. this.lines.length = 2 then it will loop through for the first item and then not continue, if there is more than 2 lines then it will still go through once and stop. This means that my total is always the same as the the value of line 1.

here is the code...

Code:
var lineTotals = 0;
alert("num lines " + this.lines.length);
for(i=0;i<this.lines.length;i++){
      var tmp = parseFloat(unformat(this.lines[i][5]));
       lineTotals += (parseFloat(lineTotals) + parseFloat(tmp));
        }


The unformat function is like this
Code:
function unformat(sText){
    var unformatted = "";
    for (i = 0; i < sText.length; i++)
    {
    //alert(sText.charAt(i));
    if(sText.charAt(i) != ",")
    {
     unformatted += sText.charAt(i);
    }
    }
    return (unformatted);
}

It seems really odd to me as i cannot see why the loop is being ended, could the parseFloat cause a problem with the 'i' indice in the loop?
Could the unformat function be a problem as it is not part of the JS object where the data is?

any suggestions would be great thanks.

Jez
 
I think you are right !!

It now seems to be going through the loop correctly and i should know better than not defining my variables locally, in my defense i am doing most of this work in languages where there is more than just GLOBAL scope, but still no excuse.

Thanks again !!!! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top