Multi-dimensional array iteration causes browser to freeze?

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


Your code so far

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;
  var cm;
  var m;
  var i;
  var j;
  var arr_len;

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

  /* this block causes browser to freeze....
    arr_len = cm/range[i];
  return m;
  //?? - find smallest common element of all arrays in m
  //return sc; 


Your browser information:

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.

Link to the challenge:

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.

Thanks for your reply Leslie!
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.

Thanks again for your help!!

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.

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.

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

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

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