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

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

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