Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

For Loop Scoping with 2D Array

I am working on an Advent Code Challenge and have run into a hall. I solved this error before, but in this instance, I am stuck. The following code is giving me a Uncaught TypeError: Cannot read properties of undefined. in relation to switch (diagArray[i][j]).

My thought is that diagArray is out of scope. Is that true?

Please, any help is great!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const diagArray = [
  [0,1,0,0,0,1,1,1,0,0,0,1],
  [1,1,0,1,0,0,0,0,0,0,0,1],
  [1,1,1,0,0,1,0,0,1,0,1,1]
 ]; // example data
  
  for(let i = 0; i < diagArray.length; i++) {
  for (let j = 0; j < diagArray.length; i++) {
    let gammaRate = ''
    let epsilonRate = ''
    let ones = 0
    let zeros = 0
    let rates = []

    switch (diagArray[i][j]) {
      case 1:
        ones++
        break
      case 0:
        zeros++
        break
      default:
        break
    }

    if (ones > zeros) {
      gammaRate+=1
      epsilonRate+=0
    } else {
      gammaRate+=0
      epsilonRate+=1
    }

    rates.push([gammaRate, epsilonRate])
    // console.log(rates)
  }
}

>Solution :

for(let i = 0; i < diagArray.length; i++) {
  for (let j = 0; j < diagArray.length; i++) {

This here has multiple problems.

The first for loop is fine, but the second one needs be bounded by the length of the inner array:

diagArray[i].length

And it’s also currently incrementing the first loops counts with i++. That needs to be j++


Working example:

const diagArray = [
  [0,1,0,0,0,1,1,1,0,0,0,1],
  [1,1,0,1,0,0,0,0,0,0,0,1],
  [1,1,1,0,0,1,0,0,1,0,1,1]
 ]; // example data
  
for (let i = 0; i < diagArray.length; i++) {
  for (let j = 0; j < diagArray[i].length; j++) {
    let gammaRate = ''
    let epsilonRate = ''
    let ones = 0
    let zeros = 0
    let rates = []

    switch (diagArray[i][j]) {
      case 1:
        ones++
        break
      case 0:
        zeros++
        break
      default:
        break
    }

    if (ones > zeros) {
      gammaRate+=1
      epsilonRate+=0
    } else {
      gammaRate+=0
      epsilonRate+=1
    }

    rates.push([gammaRate, epsilonRate])
    console.log(rates)
  }
}

Also it’s usually not advisable to have loops like this for iterating arrays, since, as you’ve discovered, it’s very easy to screw up the indices.

I recommend interating over the values of the arrays instead:

for (const row of diagArray) {
  for (const value of row) {
const diagArray = [
  [0,1,0,0,0,1,1,1,0,0,0,1],
  [1,1,0,1,0,0,0,0,0,0,0,1],
  [1,1,1,0,0,1,0,0,1,0,1,1]
 ]; // example data
  
for (const row of diagArray) {
  for (const value of row) {
    let gammaRate = ''
    let epsilonRate = ''
    let ones = 0
    let zeros = 0
    let rates = []

    switch (value) {
      case 1:
        ones++
        break
      case 0:
        zeros++
        break
      default:
        break
    }

    if (ones > zeros) {
      gammaRate+=1
      epsilonRate+=0
    } else {
      gammaRate+=0
      epsilonRate+=1
    }

    rates.push([gammaRate, epsilonRate])
    console.log(rates)
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading