# Hackerrank“Find the Factor”（在 javascript 中）由于时间限制而失败？Hackerank “Find the Factor” (in javascript) is failing because of time limit?

``````function pthFactor(n, k) {
let arr = [];
for (let i = 1; i <= n; i++) {
if (n % i === 0) {
arr.push(i);
}
if (arr.length === k) {
return arr[arr.length - 1];
}
}
if (arr.length !== k) {
return 1;
}
};
``````

``````var kthFactor = function(n, k) {
let factors = [1]
for(let i=2;i<=Math.floor(n/2);i++){
if(n%i == 0) factors.push(i)
}
factors.push(n)

return factors.length < k?-1:factors[k-1]
};
``````

## 2 个回复2

### ===============>>#1 票数：0

`` var kthFactor = function(n, k) { let bigFactors = []; let quotient = n + 1; for (let factor = 1; factor < quotient; factor++) { if (n % factor === 0) { quotient = n / factor; k--; if (k <= 0) return factor; if (factor >= quotient) break; bigFactors.push(quotient); } } return bigFactors[bigFactors.length - k] ?? -1; }; // A few test cases: console.log(kthFactor(12, 3)); // 3 console.log(kthFactor(7, 2)); // 7 console.log(kthFactor(16, 5)); // 16 console.log(kthFactor(27, 5)); // -1 console.log(kthFactor(927, 1)); // 1``

### ===============>>#2 票数：0

` `const kthFactor = (n, k, f = 1, r = []) => f * f > n ? r [k - 1] ?? -1 : n % f == 0 ? k == 1 ? f : kthFactor (n, k - 1, f + 1, f * f == n ? r : [n / f, ...r]) : kthFactor (n, k, f + 1, r) console .log (kthFactor (12, 3)); //=> 3 console .log (kthFactor (7, 2)); //=> 7 console .log (kthFactor (16, 5)); //=> 16 console .log (kthFactor (27, 5)); //=> -1 console .log (kthFactor (927, 1)); //=> 1``

### 你快到了！这里有一些建议

#### 你是对的，停在 sqrt(n) 是有帮助的

` `function pthFactor(n, k) { let arr = []; for (let i = 2; i <= n; i++) { while (n % i === 0) { arr.push(i); n = n / i if (arr.length ===k){console.log("factors so far: ", arr, " and remaining product is ", n); return i} if (i*i>n) return 0 } } }; console.log(pthFactor(3*5*7*11*13*17,4))``

## 未解决问题？本站智能推荐：

2回复

### 电子商店hackerRank挑战在Javascript中

Hi I did not pass the all the tests, only 9/16.嗨，我没有通过所有测试，只有 9/16。 so I want to know what is the problem in my code problem link: https://www.hackerr
3回复

### HackerRank对角差的时间复杂度

Can anyone help me identify the time complexity of the following code?谁能帮我确定以下代码的时间复杂度？ Background: this is a HackerRank algorithm problem where the e
2回复

### Hackerrank算法挑战问题-在hackerrank环境（节点）中显示类型错误，但在浏览器控制台上工作正常。怎么了？

function birthdayCakeCandles(n, ar) {let max = 0;for(let i = 0; i < ar.length; i++) { if(ar[i] > max) { max = ar[i]; }}let count =
3回复

### Javascript有办法做“find_if”或“FirstOrDefault”吗？

I'm a Junior-level JavaScript developer and I find that I often have situations where I need to do the equivalent of 我是一名初级JavaScript开发人员，我发现我经常遇到需要做
1回复

### karatsuba大量乘法在javascript中失败

I've tried two versions of a karatsuba multiplcation algorithm and they both fail with different results when I test them with large X and Y values.
2回复

### 计算阶乘超过JavaScript中的最大整数限制

I am trying to create a function for calculating factorial of a given number. 我正在尝试创建一个函数来计算给定数字的阶乘。 It works fine until I pass a number greater than