# 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

On my machine the code below generates the correct answers:

``````
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++) {
dividible_array.push(n);
calculatable_array.push(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) {
results.push(idx);
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 )) {
break;
}
}
}

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

}

smallestCommons([23,18])
``````

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

Challenge: Smallest Common Multiple

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.