Can we get rid of the while loop here?

There is a function for finding the sum of all the minimum identical elements of an array. Accepts an array of positive integers. Is it possible to do without
while
?
function solution(n) { while (!n.every(x => x == n[0])) { n.sort((a, b) => b  a).forEach((v, i) => n[i  1] > m ? n[i  1] % m != 0 ? n[i  1] = n[i  1] % m : n[i  1] = m : v, m = Math.min(...n)) } return n[0] * n.length }
Can the nested loop be restarted withreduce ()
or by adding a condition toforEach ()
?JavaScript Anonymous, Jan 22, 2019 
No matter how I tried, I still did not understand what the task was, but after rewriting the code, I threw away a number of useless checks and equally useless sorting, and also, technically, got rid of the wile.)
Option 1(() => {
const solution = (arr, iteration = 1) => {
if(arr.every(num => num === arr[0])) {
return arr[0] * arr.length;
}
const minNum = Math.min(...arr)
arr.forEach((num, i) => {
if(num > minNum) {
arr[i] = num % minNum  minNum;
}
});
console.log(`Iteration ${iteration}`, arr);
return solution(arr, iteration + 1);
}
console.log(solution([6,9,21]));
console.log(solution([56, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 142]));
})()Anonymous
1 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!