Smallest Common Multiple/Help [SOLVED]

Hi,

I can’t figure out what is wrong with my code. I would appreciate any kind of help.

function smallestCommons(arr) {
  var lcm = 1;
  function get_gcd(a,b){
    var g = Array.from(arguments).sort(function(a,b){
      return a - b;
    });
    if (g[0] === 0){
      return g[1];
    }
    else {
      return get_gcd(g[0], g[1] % g[0]);
    }
    
  }
  function get_lcm(a,b){
    lcm = (a * b) /get_gcd(a,b);
  }
  var h = Array.from(arr).sort(function(a,b){
    return a - b;
  });
  for (var s = 0; h[0]+s <= h[1]; s++){
    return get_lcm(h[0]+s, lcm);
  }
  return lcm;
}


smallestCommons([1,5]);

I think the

return get_lcm(h[0]+s, lcm);

should be

get_lcm(h[0]+s, lcm);

The problem you’re having is when you get to that for loop at the end of the function, you’re returning the value of get_lcm(), which is a function that doesn’t return a value.

That still doesn’t give the right answer, but that’s why you were just getting undefined when you called the function.

Yeah, it was the return Code works now, thanks a lot.