I have the same bit of code written in two different ways, and the only difference between them is that one uses a for loop, while the other uses a foreach loop. However, they seem to give different results.
Images of the results can be found at the bottom.
The bit of code checks if the blue rectangles intersect with the purple or the gray ones, and if they do, return false. This bit of code is run each time a new blue rectangle is created
The purpose of this is to make sure no blue rectangles are outside the area delimited by the gray area nor inside the green area
const USE_FOR = true;
// wall.type: string value, possible values: 'bound', 'base', 'random'
// CONFIG.wall.bound_type: 'gray'
// CONFIG.wall.base_type: 'base'
// 'bound' type: gray rectangles
// 'base' type: purple rectangles
// 'random' type: blue rectangles
if (USE_FOR) {
for (let i = 0; i < walls.length; i++) {
let wall = walls[i];
if (wall.type == CONFIG.wall.base_type || wall.type == CONFIG.wall.bound_type) {
if (isCollision(wall.getArea(), area)) {
return false;
}
}
}
} else {
walls.forEach((wall) => {
if (wall.type == CONFIG.wall.base_type || wall.type == CONFIG.wall.bound_type) {
if (isCollision(wall.getArea(), area)) {
return false;
}
}
});
}
return true;