Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
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!

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)
  }
}

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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)
  }
}

about 4 years ago · Juan Pablo Isaza Report

0

It's because in the second for loop it says diagArray.length, and it should be diagArray[i].length so that you are getting the length of the second array.

about 4 years ago · Juan Pablo Isaza Report

0

Your second iteration was increasing the i variable instead of j while iterating to the length of diagArray instead of diagArray[i].

You can use the Console API to debug your values to find out where the issue originates from without having to guess.

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 = []
    
    console.log(`${i}:${j} = ${diagArray[i][j]}`)

    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)
  }
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!