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

178
Visualizações
Unable to access variable declared inside function from if body

I'm trying to access a variable, which I declared at the top of the function from inside of an if statement (also in that same function). This is the code I wrote:

function getAround(x: number, y: number): number {
  console.log({x, y});
  let around: number = 0;
  const max = (props.size - 1);
  console.log({around});
  // top
  if (y > 0 && grid[y - 1][x].bomb) {
    console.log({max: this.max});
    around++;
  }
  // top right
  if (y < 0 && x > max && grid[y - 1][x + 1].bomb) {
    around++;
  }

  //right
  if (x < max && grid[y][x + 1]) {
    around++;
  }

  //bottom right
  if (y < max && x < max && grid[y + 1][x + 1]) {
    around++;
  }

  //bottom
  if (y < max && grid[y + 1][x]) {
    around++;
  }

  //left bottom
  if (y < max && x > 0 && grid[y + 1][x - 1]) {
    around++;
  }

  //left
  if (x > 0 && grid[y][x - 1]) {
    around++;
  }

  //top left
  if (y > 0 && x > 0 && grid[y - 1][x - 1]) {
    around++;
  }
  return around;
}

For some reason, it fails when trying to increase around, so I tried creating a simpler version:

function simple(x: number, y: number): number {
  let around: number = 0;
  if (x > y) {
    around++;
  }
  return around;
}

The simple version works for some reason. From my understanding, both of these should work fine though, right? Here is the error I get:

Error while mounting app: TypeError: Cannot read properties of undefined (reading '1')
    at getAround (PlayingField.vue:89)
    at PlayingField.vue:50
    at Array.forEach (<anonymous>)
    at PlayingField.vue:50
    at Array.forEach (<anonymous>)
    at getAllAround (PlayingField.vue:49)
    at generateGrid (PlayingField.vue:41)
    at setup (PlayingField.vue:45)
    at callWithErrorHandling (runtime-core.esm-bundler.js:6708)
    at setupStatefulComponent (runtime-core.esm-bundler.js:6317)

Line 89 contains the following code:

console.log({max: this.max});

I'm unsure if this is important, but I am using Nuxt 3 and the code is inside a script setup tag.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The line mentioned in the error is off by one and the error actually was in line 88. After fixing my if statement it now works flawlessly.

For future reference: The browser used was Edge (Chromium) version 96.0.1054.41 on Windows 10.

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