mattHunting10 - 4 months ago 20

Javascript Question

Consider this:

`//finalArray = [];`

//rangeArr = [1,2,3,4,5]

//allMultiplesArr = [[1,2,3,4...100],[2,4,6,8...100],[3,6,9,12...100],[4,8,12,16...100],[5,10,15,20...100]]

for(var m=0;m<allMultiplesArr.length;m++)

{

for(var n =0;n<allMultiplesArr[m].length;n++)

{

for(var l =0;l<=rangeArr.length;l++)

{

if(allMultiplesArr[m][n] % rangeArr[l] == 0)

{

///finalArray.push(allMultiples[m][n])

}

}

}

}

I have a 2D array

`finalArray`

`rangeArr`

`allMultiplesArr[m][n]`

Then I am aiming to make the statement: if the numbers of each inner array (

`allMultiplesArr[m][n]`

`rangeArr`

`allMultiples[m][n]`

`finalArray`

What my code currently does, is that it pushes every single number that is evenly divided by ANY of the elements in

`rangeArr`

`rangeArr`

`finalArray`

Answer

Use javascript every function to check weather all elements match the condition.

```
for(var m=0;m<allMultiplesArr.length;m++) {
for(var n =0;n<allMultiplesArr[m].length;n++) {
var valid = rangeArr.every(function(range){
return (allMultiplesArr[m][n] % range == 0)
});
if(valid) {
finalArray.push(allMultiplesArr[m][n])
}
}
}
```