Potential infinite loop detected for smallest common multiple

Tell us what’s happening:
Hi there!
I think my code is working. It is for the smallest common multiple assignment.
Yet because of the while loop Im using and the huge numbers I think Freecodecamp does not want to finish the calculations :wink:

On my machine the code below generates the correct answers:

Your code so far

function smallestCommons(arr) {

/* 1. get all numbers that the smallest comon number should be dividible by and put them in an array */
var dividible_array = [];
var calculatable_array = [];

  //check the smallest number
  var smallest_number = Math.min(...arr);

  //check the biggest number
  var largest_number = Math.max(...arr);

  // add numbers between biggest en lowest number to array
    for (var n = smallest_number; n <= largest_number; n++) {

    //console.log("dividible_array: " + dividible_array);

  // pick the lowest value and increase it with itself (always increase with the number itself)
  // if now there are two numbers the same, pick the next number that is the smallest_number
  // keep adding until all numbers are the same. When they are all the same that is the LCM

  // test with this function if all values in the array are equal
  const allEqual = arr => arr.every( v => v === arr[0] );

  // test with this function at which index the smallest value is
  function find(needle, haystack) {
      var results = [];
      var idx = haystack.indexOf(needle);
        while (idx != -1) {
            idx = haystack.indexOf(needle, idx + 1);
      return results;

  // loop with this trhough the dividible_array until all values are the same

  while (allEqual( calculatable_array ) == false) {

    var new_smallest_number = Math.min(...calculatable_array);
    var indexOfSmallest = find(new_smallest_number, calculatable_array);
    //console.log("indexOfSmallest array: " + indexOfSmallest);

    for (var i = 0; i < indexOfSmallest.length; i++) {
      var smallestValue = calculatable_array[indexOfSmallest[i]];
      var newValue = smallestValue + dividible_array[indexOfSmallest[i]];
      calculatable_array[indexOfSmallest[i]] = newValue;
      //console.log("test2:" + calculatable_array);
      if (allEqual( calculatable_array )) {

  //console.log("lasttest:" + calculatable_array);
  var resultvalue = calculatable_array[0];
  console.log("resultvalue: " + resultvalue);
  return resultvalue;



Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0.

Challenge: Smallest Common Multiple

Link to the challenge:

do the tests run? a warning is just a warning, you need to see if the tests run

Hi Guy,
Well i’m not sure under what parameters the freeCode tests work, but you have to think that’s not because a code passes that it means it’s good.

I analyzed your code, it has a lot of details that could be improved. A test for you to prove is the following: take the line idx = haystack.indexOf(needle, idx + 1); and modify by idx = -1 well … the test will pass making the existence of the check useless unecessarily large.

This warning that is given means that the executation of your code taking ‘too long’ compared to the test parameter, it isn’t a problem nowadays, its a difference of ms. But in your code there could be fewer iterations ( There are a lot of for’s and while’s, in addition to creating arrays ans callbacks ) in order to take out the complexity of it would make it cleaner and more readable.

A source of inspiration for other references and readings: https://en.wikipedia.org/wiki/Time_complexity

Another observation a code is like text, commets are good practice but for a small code it could speak for itself whithout so much comment. When there are a lot of comments there is something strange.