0

So I have this for loop, I'm stuck in the second one, but If I am, I'd also be stuck in the first one, whats the problem here?

for(var i = 0; i < oldtds.length;i += 3)
{
    var oldNameIndex = sameName(oldtds[i].innerHTML, nameList);
    if(oldNameIndex != -1)
    {
        //This means the name already exists
        //I need to combine the times, and remove the tr from the table for the second name
        //this loop accesses all the times
        for(var j = i + 1; j < oldtds.length; j += 3)
        {
            //This won't quite work, There are colons between them
            var timerArray = oldtds[j].innerHTML.split(":");
            timerArray.push(oldtds[oldNameIndex + 1].innerHTML.split(":"));
            console.log(timerArray);
            console.log("this is j " + j);
        } 
    }
}
Blake Thingstad
  • 1,639
  • 2
  • 12
  • 19
Dean
  • 57
  • 1
  • 12
  • what is this variable's value `oldtds.length` – Arpit Solanki Jun 12 '17 at 18:55
  • 2
    Assuming that `oldtds` is an array, you'd be much better off looping through it with `oldtds.forEach()`, which will allow you to not have a looping variable, which is most definitely why you have an infinite loop now. – Scott Marcus Jun 12 '17 at 18:55
  • What is sameName(), is it synchronous? – Cheloide Jun 12 '17 at 18:58
  • @ScottMarcus No, `.forEach` doesn't cut it when looping every third item. And even when it did, `for … of` would be a much better solution – Bergi Jun 12 '17 at 19:24
  • What exactly is `oldts`, and what exactly are you doing inside the loop body? Notice that nodelists or html collections can be live, and change their `.length` when you manipulate the DOM. – Bergi Jun 12 '17 at 19:27
  • Please do some debugging. Log the values of the variables. Where is the problem? – Bergi Jun 12 '17 at 19:27
  • @Bergi except they for...of isn't fully supported – Scott Marcus Jun 12 '17 at 19:44
  • Note that forEach is not as fast as a traditional for loop. May not always matter, but important to keep in mind. https://stackoverflow.com/questions/43821759/why-array-foreach-is-slower-than-for-loop-in-javascript – SethWhite Jun 12 '17 at 20:21
  • data type and context are missing :-/ – Isabelle Jun 12 '17 at 20:25
  • @SethWhite On their surface, no loop is ever going to be faster than a counting loop, simply because of the simplicity of moving sequentially via a numeric index. That, however, should not really be an issue to favor it over other looping structures. – Scott Marcus Jun 12 '17 at 20:55
  • I'm making a dynamic list that only ever has 5, I'm keeping track of them with oldtds. That part works fine, in the loop, when I log j, it stays as 1, I don't know why. – Dean Jun 13 '17 at 13:37

0 Answers0