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

150
Views
Why does my collRect array isn't getting items?

I joigned recently a fullstack course. It's going great so far(even tho it's really hard), even in JS where I used to be stuck a lot.. Well, that's what I tought, right now I'm totally lost on an exercice.

Can anyone check this fiddle and tell me where my mistake(s) is ?

The goal of the exercice is to make a classe (rectangle), a method to check collision (that's ok), the part where I have a problem is where I need to check the collision of 1000 rectangles of random sizes, their creations is okay but the check is where I am stuck.

let randomRect = [];
let collRect = [];

function colCheck(n) {


    for (let i = 0; i < n; i++) {

        randomRect[i] = Rectangle;
        Rectangle = {};
        Rectangle.name = "Rectangle " + i;
        Rectangle.topLeftXPos = Math.floor(Math.random() * 10);
        Rectangle.topLeftYPos = Math.floor(Math.random() * 10);
        Rectangle.width = Math.floor(Math.random() * 10);
        Rectangle.length = Math.floor(Math.random() * 10);

        randomRect.push(Rectangle);
    }
    for (let j = 0; j > n; j--) {
        if (randomRect[i].collides(randomRect[j])) {
            collRect.push(Rectangle);
            console.log("Collision detected")
        }
    }
}
colCheck(1000);
console.log(collRect);

The collides method is on the jsfiddle. Sorry if I made some spelling mistakes.

Here's my fiddle : https://jsfiddle.net/ou5wyrp8/3/

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Hi I started to make some edits to push you in the right direction. Your first mistake was the way you try use a class. When creating objects from classes, you want to use the new keyword, the class name, and then treat the class name as a function. Note that the function the executed when using new is the constructor e.g

let newRect = new Rectangle(topLeftXPos, topLeftYPos, width, length)

Next, there was problems in your for loop. For starters, I assumed that you wanted the j loop to be nested inside the i loop, which it was not.

I went as far to fix these two things. All that is left is for you to loop through randomRect and push collisions to collRect

class Rectangle {
    constructor(topLeftXPos, topLeftYPos, width, length) {
        this.topLeftXPos = topLeftXPos;
        this.topLeftYPos = topLeftYPos;
        this.width = width;
        this.length = length;
    }
    collides(otherRectangle) {
        if (this.topLeftXPos === otherRectangle.topLeftXPos) {
            return true;
        }

        else if (this.topLeftYPos === otherRectangle.topLeftYPos) {
            return true;
        }
        else {
            return false;
        }
    }
}

let randomRect = [];
let collRect = [];
function colCheck(n) {


    for (let i = 0; i < n; i++) {
        //populate randomRect
       let newRectangle = new Rectangle(
         //top
         Math.floor(Math.random() * 10),
         //left
         Math.floor(Math.random() * 10), 
         //width
         Math.floor(Math.random() * 10),
         //length
         Math.floor(Math.random() * 10))
        randomRect.push(newRectangle);
    }
    console.log(randomRect)
    for( let i = 0; i <n;i++){
      for (let j = n-1; j > 0; j--) {
          if (randomRect[i].collides(randomRect[j])) {
              collRect.push(randomRect[i]);
              console.log("Collisaaaion detected")
          }
      }
    }
}
colCheck(1000);
console.log(collRect);
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!