# Smallest Common Multiple - Overflow problem?

**URL:** <https://forum.freecodecamp.org/t/smallest-common-multiple-overflow-problem/229110>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [September 23, 2018, 2:12am UTC](https://forum.freecodecamp.org/t/smallest-common-multiple-overflow-problem/229110 "2018-09-23T02:12:39Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![safwyl](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/safwyl/32/100724_2.png) [@safwyl](https://forum.freecodecamp.org/u/safwyl)\
**Post date:** [September 23, 2018, 4:04am UTC](https://forum.freecodecamp.org/t/smallest-common-multiple-overflow-problem/229110/3 "2018-09-23T04:04:23Z")

</div>

Yeah I ended up switching to Euclid’s algorithm for my final solution since this wasn’t working. Don’t think it’s worth debugging this really inefficient alternative since we know Euclid’s will be exceptionally better.

```javascript
function smallestCommons(arr) {
  if (arr[0] < arr[1]) {
    arr.reverse();
  }
  var i = arr[0];
  var j = arr[1];
  var prod = 1;
  
  console.log(arr);
  var btwn = [];
  for (let k=i; k>=j; k--) {
    btwn.push(k);
    prod*=k;
  }
  console.log(btwn,prod);

  for (let f=0; f<btwn.length; f++) {
      console.log(btwn[f]);
    }
  function gcd(x,y) {
    if (y == 0) {
      return x;
    } else {
      return gcd(y,x%y);
    }
  }
  var lcm = btwn[0];
  for (let f=0; f<btwn.length; f++){
    var GCD = gcd(lcm,btwn[f]);
    lcm = (lcm*btwn[f]) / GCD;
  }
  
  return lcm;
}

smallestCommons([23,18]);

```

---

_[View the full topic](https://forum.freecodecamp.org/t/smallest-common-multiple-overflow-problem/229110)._
