How to run for loop until i === certain variable

function smallestCommons(arr) {
  var min = Math.min.apply(null, arr);
  var max = Math.max.apply(null, arr);
  var multiple = 0
  for (let i = max; i === multiple; i++) {
    if (i % min === 0 && i % max === 0) {
    multiple = i
  }
}

}


smallestCommons([1,5]);

How can I make this code so it runs until i === multiple? It doesnt work like this

You are trying to replicate a while loop. That is a dangerous approach with a value that continues increasing.

You can

  1. provide a maximum upper value
  2. rework the approach to use GCD and LCM instead
1 Like