Four Divisors LeetCode solution in JavaScript

1
var sumFourDivisors = function (nums) {
2
let check = (num) => {
3
let divs = [num]; // init the array with the number itself
4
let orig = num;
5
num = num >> 1; // divide in half to avoid checking too many numbers
6
while (num > 0) {
7
if (orig % num === 0) divs.push(num);
8
num--;
9
if (divs.length > 4) return 0;
10
}
11
if (divs.length === 4) {
12
return divs.reduce((a, b) => a + b, 0);
13
}
14
return 0;
15
};
16

17
let total = 0;
18

19
for (let num of nums) {
20
total += check(num);
21
}
22

23
return total;
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0