Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!