Smallest Common Multiple Challenge: Last test fails despite returning the solicited number

Smallest Common Multiple Challenge: Last test fails despite returning the solicited number
0.0 0

#1

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


#2

Your code will pass the tests if given enough time to complete. However, your current algorithm results in a time slow enough that the infinite loop protection prevents your code from completing the last test. All the FCC challenges can be solved with algorithms efficient enough to pass the tests, so you need to think how you can produce a more efficient algorithm.

Hint: That do while using the every array function is causing a lot of extra iterations than needed.


#3

Maybe you’re right because before asking this, instead of using the every method in the array, I used another do while loop to traverse the array, and despite giving the correct returns, it fails the last two tests.