Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

89
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda