Break a while loop. Smallest Common Multiple task

Hello.

As many people suggest, I divided the task into several smaller task. One of which is to find the greatest common divisor.
It seems the algorithm is working correctly. Logging x gives results of 104, 24, 8 and 0. But I cannot break it to return the 8.
Alternative solution is to push x into a newly created array and then return the required elements of the array (arr[arr.length-1]). But I do not like it.

function smallestCommons(arr) {
  let x  
  while (arr[0]%arr[1]!==0){
    x=arr[0]%arr[1]       // this substitution is required for the following formula to work. 
    arr[0]=arr[1]*Math.round(x)+x       //this is the formula for GCD a=b*q+r. 
console.log(x)      //this is to check results of x to make sure formula works

// following is the break statement which does not work 
if (x==0) {
      console.log(`reacher zero`) 
      break};
    arr[0]=arr[1];
    arr[1]=x;
    x=arr[1]%x
     }
     return x
}
console.log(smallestCommons([360, 128]));

I am not sure about my approach to solving the task. However, it does not matter so much at the moment. My question only concerns the break issue.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.