Array keeps coming back as undefined despite being within Scope

I am making a binary translator but have run into a scope issue.

Here is my repl: https://repl.it/@Michael_Nicol/Binary-Translator-Debug

Keep in mind that this is just a copied version of the program where I singled out the bug. There is a lot more code but I removed it so we could better figure out what the problem is.

When I go to hit run, what it should do is run a loop to go through my array. Their is two loops, one of which is embedded in the other.

  1. The first outer most loop (5) should console.log(array[i]) to display the byte.
  2. Then the embedded loop (15) should run, console.log(array[i][j]) to display each bit. For every iteration of the outer loop, this loop is completely run.
  3. Lastly, the process repeats until I run out of bytes. It should return “Finished” (21)

However, I have run into an issue. On the embedded loop for going through the bytes, it runs once and then returns that it cannot read the length of 'undefined." I have no idea why this is happeneing. I thought it was something to do with scope and I swapped let with var and still got the still issue.

  for (let j = 0; j < array[i].length; i++) 

shouldn’t it be a j++ instead of i++??

  for (let j = 0; j < array[i].length; j++) {
1 Like

oh… :man_facepalming:

Thanks.

1 Like

we’ve all made that mistake bro :rofl:

1 Like