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

84
Views
indicies after 0 are the correct object but show as undefined when accessing
class Space {
  constructor(x, y, c = 'grey',c2 = 'darkgrey') {
    this.x = x;
    this.y = y;
    this.w = 10;
    this.h = 10;
    this.c = c;
    this.c2 = c2;
  }
  draw() {
    drawSpace(this.x,this.y,this.c,this.c2)
  }
}
 
// Creates grid for movement
function createGrid() {
  for (let i = 0; i < 80; i+=1) {
    let tempArr = [];
    for (let j = 0; j < 40; j+=1) {
      tempArr.push(new Space(i,j));
    }
    grid.push(tempArr);    
  }
  grid[0][0] = pathfinder;
  grid[79][0] = target;
}

//draws grid
grid.forEach(function(e) {
  e.forEach(function(f) {
    f.draw();
  });
});

What's not shown is the implementation of the "pathfinder" and "target" variables. They are also "Space" objects with specific green/red colors respectively.

The issue arises with the line grid[80][0] = target; Where I get an error stating that I cannot set properties of undefined. (setting 0). This is referencing the '0' that should be from the 0th index of the 80th array. When console.log(grid[80][0]); I do get the base Space Object that was set in the original for loop. Why is this happening/how can I set the value of the index to the needed Space object 'target'.

Edit - The

    grid[0][0] = pathfinder;
    grid[80][0] = target;

is on the first for loop.

Edit 2 - It does it on grid[1][0] as well, I understand that the grid[80][0] doesn't exist as its going 0-79.

about 4 years ago · Juan Pablo Isaza
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!