Smallest Common Multiple - code breaking down?

Tell us what’s happening:
so this passes for all but one test case, sometimes the second to last test case doesnt pass then i press run again and it does so it made me think is it having too many calls?
i did a console.log of the while loop and it doesnt look like its exiting on the conditions set. if thats not the case as i cant see what its doing anyway im kinda stuck for ideas to keep going. im in my 30s its been a while since ive done maths related stuff so without being able 2 see a log of my variables its getting tricky.
if this is a too many calls situation (am i even describing it correctly) what would be a way to increase the efficiency?

Your code so far


function smallestCommons(arr) {
  arr.sort((a,b) => a > b)
  var newArr = Array(arr[1]).fill().map((x, i) => i+1).filter(x => x >= arr[0])
//console.log(newArr);
let i = newArr.length-1
let num = arr[1]

while (i > 0){

  //  console.log('arr:',newArr[i],'num:',num, num%newArr[i])
    if (num%newArr[i] === 0){
      i--
    } else if (num%newArr[i] !== 0){
      num++
     i = newArr.length-1
    }

}

  return num;
}


console.log(smallestCommons([23,18]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple

Yea, it sounds like your code is taking too long. There’s an infinite loop protection when running the challenge tests I believe, to prevent browser freezing and such. So you will probably have to find a way to make your code more efficient.

what a nightmare i dont understand why 1,13 works but 18,25 i would think the longer array would cause the infinite loop protection to trigger.

No, the last test will take the longest to get to. The return there is over 6 million. And you are counting to it like one at a time. Perhaps if you didn’t count so many numbers. Maybe if you count up to it by 23 at a time, since you know it has to be a multiple of 23 - or whatever the largest in the array is.

alrite ty for the tip kinda cheating but ohwell unfortunately that didnt fix my error it did get it closer to the number wanted but it cuts off at 2018940

function smallestCommons(arr) {
  arr.sort((a,b) => a > b)
  var newArr = Array(arr[1]).fill().map((x, i) => i+1).filter(x => x >= arr[0])
//console.log(newArr);
let i = newArr.length-1
let num = arr[1]
//console.log(arr[1])
while (i > 0){

    //console.log('arr:',newArr[i],'num:',num, num%newArr[i])
    if (num%newArr[i] === 0){
      i--
    } else if (num%newArr[i] !== 0){
     num += arr[1]
     i = newArr.length-1
    }

}

  return num;
}

i also swapped around the if statments so the one which is more likely to be triggered is tested first but that did not help in anyway