I am doing the Smallest Common Multiple code challenge. Before going to the code, I need to explain that to aboard this challenge, I used an array approach, so in a do-while piece, I get the first number who is divisible by all the elements in the range, who are in an array constructed with the given parameters.
The thing is, every test passes except for the last one, despite the number returned is the correct number to pass the test.
I am testing my code on the Opera Console before testing it on FCC.
Code
function smallestCommons(arr) {
var numMultiplierAll = 1;
var foundNumber = false;
var multipliersArray = [];
/* If the first element of the array in the parameter is bigger
than the second one,the array is reversed */
if (arr[0] > arr[1]) {
arr = arr.reverse();
}
/* Multipliers Array with all the numbers in the range
is created */
for (var multipliersArrayElement = arr[0];
multipliersArrayElement <= arr[1];
multipliersArrayElement++) {
multipliersArray.push(multipliersArrayElement);
}
/* Starting with 1, this do-while cycle is executing until it finds
a number who is divisible by all the numbers in the range */
do {
foundNumber = multipliersArray.every(function(element) {
return isMultiplier(numMultiplierAll, element);
});
if (foundNumber) {
break;
} else {
numMultiplierAll++;
}
} while (!foundNumber);
return numMultiplierAll;
}
/* Auxiliar function to check if an a value is a multiplier of b value */
function isMultiplier(a,b) {
return (a % b == 0);
}
console.log(smallestCommons([23,18]));
Browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.170 Safari/537.36 OPR/53.0.2907.99
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple