Smallest Common Multiple I need help with my life

Tell us what’s happening:

My head almost exploded trying to solve this challenge, then I managed to solve it, I passed the challenge and I was happy as you have no idea, excitement on a large scale.
Then I wanted to check the answers of the “hint” and my head exploded again, I almost killed myself. I can’t fully understand what I felt when I saw how short and simple the answers were, disappointment, frustration (they weren’t that simple either, but I couldn’t think of anything like that and at this point I feel that I should have done something better than what I did).
I wish you would look at my code and tell me what you think. Mainly what I want is feedback on my code, some advice or criticism in general.

Merry Christmas, I appreciate your help.

Your code so far


function smallestCommons(arr) {
let arrMax = Math.max(...arr);
let arrMin = Math.min(...arr);
let range = [];
let acum2 = [];
let acum3 = [];
let acum5 = [];
let acum7 = [];
let acumDivin = [];
let multiplies = [];
let result = 0;

for (let i = arrMin; i <= arrMax; i++) {
  range.push(i);
}
/******* Multiples of each number are obtained. ********/
range.forEach(item => {
  let dividend = item;
  let temp2 = [];
  let temp3 = [];
  let temp5 = [];
  let temp7 = [];
  
  while (dividend > 1) {
    if (dividend % 2 == 0) {
      temp2.push(2);  
      dividend /= 2;
    }else if (dividend % 3 == 0) {
      temp3.push(3); 
      dividend /= 3;
    }else if (dividend % 5 == 0) {
      temp5.push(5);
      dividend /= 5;
    }else if (dividend % 7 == 0) {
      temp7.push(7);
      dividend /= 7;
    }else if (dividend % dividend == 0) {
      acumDivin.push(dividend);
      dividend /= dividend;   
    }
  }
  if (temp2.length > acum2.length) {
    acum2 = [...temp2]
  }
  if (temp3.length > acum3.length) {
    acum3 = [...temp3]
  }
  if (temp5.length > acum5.length) {
    acum5 = [...temp5]
  }
  if (temp7.length > acum7.length) {
    acum7 = [...temp7]
  }
  
})
/************** *************************/
multiplies = [].concat(acum2, acum3, acum5, acum7, acumDivin);
result = multiplies.reduce((a,b) => {
  return a * b;
})

return result;
}

smallestCommons([1, 5]); 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Smallest Common Multiple

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple

your code works, right?
let’s start with that, it is a great accomplishment.
everything else comes with practice and exposing yourself with different methods of solving things

You can certainly make your code better, you can start with a best practice thing, you could try to have DRY code, where DRY stand for Don’t Repeat Yourself: there are many parts in your code where you have various subsequent lines where your code does the same thing changing just one or two parameters, you can try compressing that! (loops or loop-like methods usually work well for that)

I tried to “make it better”, I don’t know if it’s better or if it’s the same, I couldn’t figure out how to get rid of the huge amount of “ifs”. What do you think?

function smallestCommons(arr) {
  let arrMax = Math.max(...arr);
  let arrMin = Math.min(...arr);
  let range = [];
  let acum2 = [];
  let acum3 = [];
  let acum5 = [];
  let acum7 = [];
  let acumDivin = [];
  let multiplies = [];
  let result = 0;
  
  for (let i = arrMin; i <= arrMax; i++) {
    range.push(i);
  }

  function multpliesVerify(dividend, temp, divisor) {
    temp.push(divisor);
    return dividend /= divisor;
  }
/******* Multiples of each number are obtained. ********/
  range.forEach(item => {
    let dividend = item;
    let temp2 = [];
    let temp3 = [];
    let temp5 = [];
    let temp7 = [];
    
    while (dividend > 1) {
      if (dividend % 2 == 0) {
        dividend = multpliesVerify(dividend, temp2, 2)
        // temp2.push(2);  
        // dividend /= 2;
      }else if (dividend % 3 == 0) {
        dividend = multpliesVerify(dividend, temp3, 3)
        // temp3.push(3); 
        // dividend /= 3;
      }else if (dividend % 5 == 0) {
        dividend = multpliesVerify(dividend, temp5, 5)
        // temp5.push(5);
        // dividend /= 5;
      }else if (dividend % 7 == 0) {
        dividend = multpliesVerify(dividend, temp7, 7)
        // temp7.push(7);
        // dividend /= 7;
      }else if (dividend % dividend == 0) {
        dividend = multpliesVerify(dividend, acumDivin, dividend)
        // acumDivin.push(dividend);
        // dividend /= dividend;   
      }
    }

    function greatestExponent(temp, acum){
      if (temp.length > acum.length) {
        acum = [...temp]
      }
      return acum;
    }
    acum2 = greatestExponent(temp2, acum2);
    acum3 = greatestExponent(temp3, acum3);
    acum5 = greatestExponent(temp5, acum5);
    acum7 = greatestExponent(temp7, acum7);

    
  })
/************** *************************/
  multiplies = [].concat(acum2, acum3, acum5, acum7, acumDivin);
  result = multiplies.reduce((a,b) => {
    return a * b;
  })
  
  return result;
}

console.log(smallestCommons([1,7]));