Smallest Common Multiple Code

Smallest Common Multiple Code
0.0 0

#1

Tell us what’s happening:

Your code so far

function smallestCommons(arr) {
  var arr1 = [];
  var arr2 = [];
  var count = arr[0];
  for(var i=1; i<100;i++) {
    arr1.push(i*arr[0]);
    arr2.push(i*arr[1]);
  }
  for(var s=0; s<arr1.length;s++) {
    for(var t=arr[0]; t<=arr[1];t++) {
      if(arr1[s] % t ==0) {
        count++;
        console.log(count);
      }
    }
    if(count>=arr[1]) {
      return arr[s];
    } else {
      count=arr[0];
    }
    
  }
  return count;
}


smallestCommons([1,5]);
console.log("end");```
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/smallest-common-multiple

#2

Do you have a question?


#3

I can’t get the code to work and I was wandering where I went wrong


#4

It’s returning undefined. Although it should return a number


#5

In your code, you have the following if statement:

if(count>=arr[1]) {

In the test case of smallestCommons([1,5]), arr[1] is 5, so when count >= 5, you return arr[s]. The problem is when count is equal to 5, s is equal to 11 and arr[11] results in undefined, because there are only 2 elements (and not 12 elements) in arr.

I suggest you put a few more console.log statements to see what the values of each variable are at various points in your solution, to help you figure out your logic error.