Tell us what’s happening:
This code works just fine for all the fCC tests when run locally under Node.js or in Firefox/Chrome console.
Am I missing something here?
Your code so far
js
function smallestCommons(arr) {
let lcm, largestNum, largestNumMultiple, remainders;
// Create an array containing the range of numbers, from largest to smallest (inclusive) from the ones given in arr.
let revRange = function(arr) {
let rng = [];
let rngLength = (Math.max(...arr) + 1) - Math.min(...arr);
for (let i = 0; i < rngLength; i++) {
rng.push(Math.max(...arr) - i);
}
return rng;
}(arr);
largestNum = revRange[0];
largestNumMultiple = revRange[0];
do {
// Set remainders to an empty array at the loop outset.
remainders = [];
// Divide the current largestNumMultiple by each num in revRange, pushing the remainder of each result into
// the remainders array.
for (let num of revRange) {
remainders.push(largestNumMultiple % num);
}
// If every remainder of remainders === 0, the current largestNumMultiple is the least common multiple.
if (remainders.every(remainder => remainder === 0)) {
lcm = largestNumMultiple;
}
// Otherwise, add the original largestNum to the largestNumMultiple to create the next
// largestNumMultiple for the next loop.
else {
largestNumMultiple += largestNum;
}
// Keep running the loop while any remainder of remainders !== 0. Loop stops when every remainder === 0.
} while (remainders.some(remainder => remainder !== 0));
return lcm;
}
smallestCommons([1,5]);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:60.0) Gecko/20100101 Firefox/60.0
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple