Smallest Common Multiple - help, I'm stuck!

Hi everyone!

I’m currently stuck on the “Smallest Common Multiple” task and I feel I made things way to complicated, but also have no solid idea what I could do differently or what to google for to get closer to the solution. Also I don’t want to give up and just read the solution, so I decided to ask for help here.

This is my code (I tried to explain what I intended to do in the code directly rather then explaining it beforehand). It would be much appreciated if anyone can explain me what is going wrong and why it’s going wrong and whether or not I’m on the right track or I should rather start over (if that is the case, some pointers for what to do different in the next attempt would be much appreciated). Thanks in advance!

function smallestCommons(arr) {
  
  var orderedArr = arr;
  var fullNumArr = [];
  var compareArr = [];
  
  //orders arr in orderedArr, so that higher number is always the first one
  if (arr[0] > arr[1]){
    orderedArr = [arr[1], arr[0]];
  }
  
  //prepares arrays needed in findNum function
  function prep (value){ 
    //creates an array with all numbers between arr[0] and arr[1] (from largest to smallest number)
    //creates subarrays for compareArr, that will be filled up in the next for loop
    for (var i = orderedArr[1]; i >= 1; i--){
      if (i >= orderedArr[0] && i <= orderedArr[1]){
        fullNumArr.push(i);
        compareArr.push([]);
      } else {
        return compareArr;
      }
    }    
    //fills subarrays of compareArr with multiples of each number, so that they can be compared within the findNum function
    for (var j = 0; j < fullNumArr.length; j++){
      for (var k = 1; k <= value; k++){
        compareArr[j].push(fullNumArr[j] * k);
      }
    }
    return compareArr;
  }

 //findNum compares compareArr[0] and compareArr[1] to find the smallest common multiple of these 2
 //then it replaces the first two numbers in the fullNumArr with this smallest common multiple
 //then it should (but currently doens't) go through the prep function again to update compareArr
 //based on the changes made to fullNumArr. I want to repeat this process until only 1 number is left
 //in the fullNumArr array and then (in my theory) the only thing left to do would be to return that number
 function findNum (){
    prep(orderedArr[1]);
    for (var l = 0; l < compareArr.length; l++){
      if (compareArr[l].length > 1){
        for (var m = 0; m < compareArr[l].length; m++){
          for (var n = 0; n < compareArr[l+1].length; n++){
            if (compareArr.length == 1){
              return fullNumArr[0];
            }
            else if (compareArr[l][m] == compareArr[l+1][n]){
              fullNumArr[l] = compareArr[l][m];
              fullNumArr.splice(l+1, 1);
              compareArr = [];
              prep(fullNumArr[0] * fullNumArr[1]);
            }
          }
        }
      } else {
      return fullNumArr[0];
      }      
    }
  }
  
  return findNum();
}


smallestCommons([1, 5]);

Thanks for the editing and the tips along the way. I haven’t written the algorithm out in plain language, but I will try that out in the future. I think I understood what the code should do in theory (the link in included in the task description helped a lot with this), but I couldn’t figure out yet, how to write the code in order to achieve just that (apart from the solution I tried to pursue).

Sorry to hear, that my explanations weren’t sufficient. I tried to explain my thought process as well as possible. I appreciate your help, but if I’m honest, I’m still pretty lost and unsure how to proceed in order to get closer to the solution.

function smallestCommons(arr) {
  let sa = arr.sort((a,b)=>a-b);
  let sa1 = []
for (let i=sa[0];i<=sa[1];i++){
  sa1.push(i);
};
let mult = sa1.reduce((a,b)=>a*b,1);
for (let j=sa[1];j<=mult;j++){
 if (sa1.every(x => j %x ==0)){
   return j;
 }
 }
 }

This code works on console but does not pass the last two tests. Why?

The first problem is that your code is way too complicated. (I gave up on reading by looking at the structure, I haven’t even tried frankly) No offense but it’s a waste of time to modify current code to make it work because there are too many lucking errors.

This problem is happening because you are packing everything this function does in one place. In fact, you can break up the problem and have function for each of sub-problem.

I see at least three separate things.

  1. Expanding the given range argument. e.g) [5, 8] => [5, 6, 7, 8]
  2. Computing the smallest common multiple of two numbers. (**most important part)
  3. Computing the smallest common multiple of many numbers based on #2.

If you can correctly implement these ideas, the resulting answer will look much better and identifying any error will be much simpler.

So, take your time and try to implement these things piece by piece. Could you do that?

If your code produces correct answer for every test on your environment, but fails on here or fails for a large input, then it is a common problem and you can move on if you wish to.

You can read about the similar discussion from this post and on. It won’t give you the exact reason, but you will know this is a common problem for challenges of this type.

Yeah. It happened to me here 1 day ago:

I don’t know about the details but I think that this is a common issue in freecodecamp where some codes that requires some loops won’t work. It works in the console but it just doesn’t pass the tests for unknown reasons. Maybe we should report about this issue or something.