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