Breaking out of a while(true) loop


I feel really stupid asking for help on this because it should be really simple, but I can’t find the reason why this code is not behaving like I think it should, heres the code:

function smallestCommons(arr) {
  arr.sort( (a,b) => a-b);
  let flag = true;
  let count = arr[1];
  while (flag == true) {
    count += 1;
    for (let i=arr[0]; i <= arr[1]; i++) {
      if (count % i != 0) {
      if (i = arr[1] && count % i == 0) {
        flag = false;
  return count;


If you run the code you can see the console is printing false, then why is the while loop not ending if the while (flag == true) condition is not being met?

The inner loop have to finish its execution, before your while loop finish its iteration and evaluates again your condition.

You can break the loop and force its exit if you don’t want your inner loop to finish executing :slight_smile:

Hope this helps :+1:

thank for your reply.

If am not mistaken am giving the inner loop a condition to terminate.

for (let i=arr[0]; i <= arr[1]; i++) 

the i variable should increment and end the loop when i <= arr[1]. Am testing over and over and I can see that the i variable is stuck at 2 but I dont know why.

the problem is here

if (i = arr[1] && count % i == 0) 

the correct expression should be

if (i == arr[1] && count % i == 0)