Smallest Common Multiple unknown error

Smallest Common Multiple unknown error
0

#1

Hi all,

I think I have this challenge done. The code returns same results as requested but I keep on getting an error on the fcc console with higher numbers. Here is my code:

function smallestCommons(arr) {
  
  var order = arr.sort(function(a, b) {
    return a - b;
  });
  
  var first = order[0];
  var last = order[order.length - 1];
  
  var index = 1;
  for (var i = first; i < last - 1; i++) {
    order.splice(index, 0, i + 1);
    index++;
  }
  
  var start = last;
  
  function find() {
    var counter = 0;
    for (var k = 0; k < order.length; k++) {
      if (start % order[k] == 0) {
        counter++;
      }
    }
    
    if (counter == order.length) {
      return start;
    } else {
      start++;
      return find();
    }
  }
  
  find();

  return start;
}


smallestCommons([23,18]);

I tried with //noprotect and it doesn’t work either.

Could anybody check it out? thanks in advance


#2

Your recursive function maxes out the call stack. This is the error message in the console:

RangeError: Maximum call stack size exceeded at find

Looks like you are going to have to rethink your algorithm.


#3

I retyped the algorithm using a for loop instead of the recursive function. Now I get all green lights :slight_smile: thanks