Hello, please help!
I came up with a solution for the Smallest Common Multiple challenge that work when I try it locally on the browser console but when I try to run the test in challenge page it doesn’t get the last answer right… could it be because of the time it takes to calculate the answer? When running the code locally it doesn’t take long but is the only explanation I can think off. Please help.
Here is my code ( I added all the function calls at the end just for this post):
function smallestCommons(arr) {
let numbers = [];
let multiple = 2;
let result = 0;
// function to put all the numbers in a new array
let getArr = (g, l) => {
for( let i = l; i <= g; i++){
numbers.push(i);
}
};
// Just checking for the higher number
if(arr[0] > arr[1]){
getArr(arr[0],arr[1]);
}else{
getArr(arr[1],arr[0]);
}
// reverse the array to start working with highest number
numbers = numbers.reverse();
// run this until we get a result
while(result === 0){
let resultArr = [];
// divide the current multiple for each number
for(let x = 0; x < numbers.length; x++ ){
if(multiple % numbers[x] !== 0){
// if the number is not valid don't continue to the smaller numbers
break;
}else{
// if the number pass push it to a new array
resultArr.push(0);
}
}
// if all the number got passed to the new arr
if(resultArr.length === numbers.length){
//we got our multiple and we break the while loop
result = multiple;
}
//keep going searching in the loop
multiple++;
}
console.log(result);
return result;
}
smallestCommons([1, 5]); // should return a number.
smallestCommons([1, 5]); // should return 60.
smallestCommons([5, 1]); // should return 60.
smallestCommons([1, 13]); // should return 360360.
smallestCommons([23, 18]); // should return 6056820.
Thank you for your help!
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/smallest-common-multiple