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

231
Views
Accessing all elements of an array at the same time

I have two arrays containing a circle object and a wall object..

const allCircles = [circle1, circle2, circle3, circle4]; //
const allFunnelWalls = [funnelBottomLeft, funnelBottomRight, funnelLeft, funnelRight];

I have this function called collide and it takes two arrays as parameters, so far I have it working for one circle and wall because it accesses the elements from both arrays using the same index of "i" How do I create it so that I can access all the elements in the allFunnelWalls array at the same time and test it against each circle? I've tried using the every() method from arrays, nested for loops. I've reached my wits end.. help would be much appreciated

function collide(array, array2){

    for(i = 0; i < array.length; i++){

        // Collision detection algorithm between circles + inner walls
        let collision = lineCircle(array2[i].x, array2[i].y, array2[i].x2, array2[i].y2,
        array[i].x, array[i].y, array[i].radius);

        // Collision detection
        if(collision){
          array[i].colour = "green";
          array[i].xSpeed = 0;
          array[i].ySpeed = 0;
          return true;
        }else{
          array[i].colour = "red";
          return false;
        }
    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could try a nested map.

function collide(array, array2) {
  let results =  array.map(circle => {
    return array2.map(wall => {
      // Collision detection algorithm between circles + inner walls
      let collision = lineCircle(wall.x, wall.y, wall.x2, wall.y2,
         circle.x, circle.y, circle.radius);

    // Collision detection
    if(collision) {
        circle.colour = "green";
        circle.xSpeed = 0;
        circle.ySpeed = 0;
        return circle;
    }else{
        circle.colour = "red";
        return circle;
    }
    })
  })
  /* if you want to examine the results, you can do so here
  [
    { colour: 'green', xSpeed: 0, ySpeed: 0 },
    { colour: 'green', xSpeed: 0, ySpeed: 0 },
    { colour: 'green', xSpeed: 0, ySpeed: 0 }
  ],
  [
    { colour: 'red'}
  ],
  */

  return results.any(result => result.colour === "green");
}
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!