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

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

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