By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,382 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,382 IT Pros & Developers. It's quick & easy.

stuck in infinite loop

100+
P: 132
Hi guys,

I'm a bit stuck in an infinite loop. What I'm trying to do in the code below is to compare two arrays and if there's an item in array ALGEMEEN that is not in array ITEMS, I want to add it to ITEMS.

Now the ITEMS-array looks like this:

Expand|Select|Wrap|Line Numbers
  1. items=[
  2. {name:"name1", highest:0, lowest:0, current:0 },
  3. {name:"name2", highest:0, lowest:0, current:0 },
  4. {name:"name3", highest:0, lowest:0, current:0 }
  5. ];
  6.  
The ALGEMEEN-array looks like this:
Expand|Select|Wrap|Line Numbers
  1. algemeen[0] = "name1"
  2. algemeen[1] = 0
  3. algemeen[2] = "name2"
  4. algemeen[3] = 1
  5. algemeen[4] = "name3"
  6. algemeen[5] = 2
  7. algemeen[6] = "name4"
  8. algemeen[6] = 3
  9. etc...
  10.  
So below is the code I'm using to compare the two arrays and to add something to the ITEMS-array if it's not in there yet.

Expand|Select|Wrap|Line Numbers
  1. function additems()
  2. {
  3. var i;
  4. var j;
  5. for (i=0; i<algemeen.length; i+2)
  6.     {
  7.     var itemsverify=0;
  8.     for (j=0; j<items.length; j++)
  9.         {
  10.         if (algemeen[i] == items[j].name)
  11.             {
  12.             items[j].current = algemeen[i+1];
  13.             itemsverify = 1;
  14.             }
  15.         }
  16.     if (itemsverify==0)
  17.         {
  18.         alert("ALARM \n" + "algemeen " + i + "\n" + "items " + j);
  19.         items[items.length] = {name: +'"' + algemeen[i] + '"', highest:0, lowest:0, current:algemeen[i+1]};
  20.         }
  21.     }
  22. }
  23.  
Now the compare-part works, it's just the add part that doesn't work. When I try to execute the function, it gives me the alert as I requested because in my ALGEMEEN-array I have 130 items and in my ITEMS-array I have 129. So I get the alert at i=0 and j=129 but I should get it at i=258 and j=129. And j keeps on counting up. I can't get out of the loop.



Now ALGEMEEN[258] and ITEM[129].name should be the same but I removed ITEM[129] to test the function but it doesn't create ITEM[129].

So my question now is, how do I compare those two arrays and if neccessary add one into the ITEMS-array?

Best regards,
Kenneth
Nov 14 '08 #1
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,385
while looping you shouldn't modify the base-array ... this leads to unpredictable behaviour ... just create a copy where you add/delete the items and reassign that to the variable you wish when the loop is finished. you could even improve the array-compare by using a js-lookup-object ... you might have a look here for a start that shows you how to optimize array comparations ...

kind regards
Nov 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.