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

130
Visualizações
cant access index in previous array

I am trying to compare grid[i][j] with the index directly above it, then return all of the values that are different. So if grid[i][j] does not match the one above it, the code should return grid[i][j].

I thought that [i-1] would work (directly below👇 ), but i am just getting an error of cannot read properties of undefined.

if(grid[i][j] !== grid[i-1][j]){
                        return grid[i][j]
                    }

Here is the test case:

[["1","0","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]

below is the code that i have written:

var numIslands = function(grid) {
        for(let i = 0; i < grid.length; i++){
            for(let j = 0; j < grid.length; j++){
                let ans = 0;
                if(grid[i][j] === '1'){
                    if(grid[i][j] !== grid[i-1][j]){
                        return grid[i][j]
                    }
                }
                
            }
        }
    };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Does this match your desired output?

let x = [                    // Expected Outputs:
  ["1", "0", "1", "1", "0"], // _, 1, 0, _, _
  ["1", "1", "0", "1", "0"], // _, _, _, 0, _
  ["1", "1", "0", "0", "0"], // 0, 0, _, _, _
  ["0", "0", "0", "0", "0"]  // = 1, 0, 0, 0, 0
]
for (let i = 0; i < x.length; i++) {
  let lower = x.slice(i + 1, i + 2)
  for (let j = 0; j < x[0].length; j++) {
    if (!lower.length) {
      continue
    }
    if (lower[0][j] != x[i][j]) {
      console.log(lower[0][j])
    }
  }
}

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