# Multi-dimensional array iteration causes browser to freeze?

Multi-dimensional array iteration causes browser to freeze?
0

#1

Tell us what’s happening:

Hey all! I am working on the smallest common multiple problem…my plan is to make a 2D array with all of the multiples of each element in the range, up to an arbitrary multiple, then find the least common element in all of these arrays.

However, when I am populating the 2D array with nested for loops, my browser freezes, presumably due to an infinite loop.
I know there are probably better solutions out there for the scm problem, but I am really stumped on why this code would cause an infinite loop.
The last commented out block is the offending piece of code.

Any ideas??

Thanks!!
–t

``````function smallestCommons(arr) {
var sc;

var min;
min = Math.min(arr[0],arr[1]);
var max;
max = Math.max(arr[0],arr[1]);

var range;
range=[];

var cm;

var m;
m=[];

var i;
var j;

var arr_len;

for(i=0;i<=(max-min);i++){
range[i]=min+i;
}

cm = range.reduce(function(a,b){
return a*b;
});

/* this block causes browser to freeze....
for(i=0;i<range.length;i++){
m[i]=[];
arr_len = cm/range[i];
for(j=0;j<arr_len;j++){
m[i][j]=range[i]*(j+1);
}
}
*/

return m;

//?? - find smallest common element of all arrays in m

//return sc;

}

smallestCommons([1,5]);
``````

Your Browser User Agent is: `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36`.

#2

The crash happens when the difference between the two array elements is 10 or greater. I suspect that it isn’t a real infinite loop, but that the approach is so slow and/or resource intensive that it crashes.

#3

I didn’t provide the input the code fails on; it’s smallestCommons(1,5).

You must be talking about the difference in array size, i.e., for this input, m[0] has 120 elements and m[1] has 60, since the difference in value of either array should not exceed 5.

In either case, is this difference in array size problem a quirk of Javascript? It seems weird that a difference in array size should matter.
Also could you clarify what you mean by the approach being slow? This nested for loop should only run 120+60+40+30+24 times for the 1,5 input.

–t

#4

By difference I meant `arr[1] - arr[0]`.
`smallestCommons([1, 5])` will not crash.
Neither will `smallestCommons([1, 10])`.
But `smallestCommons([1, 11])` will crash and so will `smallestCommons([2, 12])`.

The larger the difference (or “range”) of the array, the slower the execution is.

#5

Hey Les, I am still having trouble with this; smallestCommons([1,5]) does actually crash.
Sorry I just can’t get past why this won’t work.

Does your statement “The larger the difference (or “range”) of the array, the slower the execution is.” apply to arrays in general or just this particular method I am using?
It seems like a difference of 10 between two array elements is relatively small to be causing problems.

#6

hey hold on I just saw this code you added in repl>it…let me look at that hold on one sec!!

#7

OK, this is what I needed to push me in the right direction!
Sometimes I just need someone to slap me over the head, and you did that, thanks very much =)

#8

I try not to slap to hard, just show you where to stand to get a different perspective.