Hey, I’ve been struggling with the smallest common multiple algorithm.
I understand the problem this way.
- I need to find the min and max numbers between the two of the items in the arr.
- Next, I need to create an array and put all of the numbers in the range between the min and max numbers there, including both of them.
- I need to create a loop which increments a number until it finds the one which’s remainder is 0 to all of the numbers in the array.
I know how to do the first two steps, but I am stuck at the last one.
I don’t know how to create a loop which will increment a number from 0 till it finds the one which’s remainder is 0 when it’s divided by each of the numbers in the array.
Any help and tips how to think would be much appreciated.
My code so far:
function smallestCommons(arr) {
let minNum = Math.min(...arr);
let maxNum = Math.max(...arr);
let mySeq = [];
for(let i = minNum; i <= maxNum; i++){
mySeq.push(i);
}
// stuck here
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/