Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda