Four Divisors LeetCode solution in JavaScript
1var sumFourDivisors = function (nums) {2let check = (num) => {3let divs = [num]; // init the array with the number itself4let orig = num;5num = num >> 1; // divide in half to avoid checking too many numbers6while (num > 0) {7if (orig % num === 0) divs.push(num);8num--;9if (divs.length > 4) return 0;10}11if (divs.length === 4) {12return divs.reduce((a, b) => a + b, 0);13}14return 0;15};1617let total = 0;1819for (let num of nums) {20total += check(num);21}2223return total;24};