I believe the code I have should technically pass this challenge. However, it uses a while loop which stops at a very large i and as a result, it is not passing the following tests:
smallestCommons([1, 13])
should return 360360.
smallestCommons([23, 18])
should return 6056820.
function smallestCommons(arr) {
arr.sort((a,b) => a-b)
let i = 1
let min = arr[0]
let max = arr[1]
let minMultiples = []
let maxMultiples = []
let commonMultiples = []
let numbersBetweenMinMax = []
for (let n = min ; n<=max ;n++) {
numbersBetweenMinMax.push(n)
}
while ( i < 1000000) {
minMultiples.push(arr[0]*i)
maxMultiples.push(arr[1]*i)
i++
}
for ( let j = 0 ; j < minMultiples.length ; j++) {
if (maxMultiples.indexOf(minMultiples[j]) > -1) {
commonMultiples.push(minMultiples[j])
}
}
for (let k = 0 ; k<commonMultiples.length ; k++) {
if (numbersBetweenMinMax.every(n => commonMultiples[k] % n == 0)) {
return commonMultiples[k]
}
}
}
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/73.0.3683.103 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple