Need help improving performance. The error message states "Execution Timed Out (12000 ms)" preventing submission of the solution.
Your task is to create a function that can determine if a given integer n is a perfect number. A perfect number is one where the sum of its divisors (excluding itself) equals the number itself. If it's a perfect number, return True, otherwise return False.
For example, for n = 28, the divisors are: 1, 2, 4, 7, 14, 28. The sum of these divisors equals 28, making it a perfect number. Therefore, you should return True.
function isPerfect(n) {
let factorSum = 0
for(let i=1; i<n; i++){
if(n%i ==0){
factorSum+=i
}
}
return factorSum ===n? true:false
}