Home > Software design >  For loop doesn't run after 1 loop
For loop doesn't run after 1 loop

Time:07-13

I'm currently working on a project that involves a lot of nested functions and loops. So far, the problems have been easy to look for and find out, but this seems to be somewhat obscure. Here's the code:

            if (c.cutscene == this.cutsceneShots[i][0]) {
                cutsceneDuration = this.cutsceneShots[i][1].duration
                for (let j = 0; j < this.cutsceneShots[i][1].length; i  ) {
                    this.cutsceneShots[i][1][j].shot(c)
                }
                break
            }
        }

The for loop is to find a function in a variable in an object in an array, cutsceneShots. The code runs once, but then errors, saying

Uncaught TypeError: Cannot read properties of undefined (reading '1')

Pointing to for (let j = 0; j < this.cutsceneShots[i][1].length; i ). The variable is always in index 1; there's no other calls to the variable.

I've tried logging the this.cutsceneShots[i] at the end of the for loop, but it returns normally. I don't know what else to do at this point.

CodePudding user response:

this.cutsceneShots[i][1]
this returning you a undefined value check and see what you are storing on index (1)

CodePudding user response:

Answer: I put i instead of j in the for loop. Thank you @The KNVB

CodePudding user response:

Using i may solve the problem

  • Related