# How to solve Leetcode No. 1492: kth Factor of N in JavaScript

I’m a visual learner and thinker. So let’s tackle this leetcode problem in a non-esoteric way.

# Problem Statement

Pretty simple when it’s not in mathy/code speak: we’re given one number (n) and another number (k). We have to return the kth factor of n. If there isn’t the kth factor, we should return -1.

# Let’s Explain

First of all, let’s remind ourselves what a factor is: a number that divides into another number exactly and without leaving a remainder.

Let’s say we’re given a number: 12. What are all its factors?

`factors: 1, 2, 3, 4, 6, 12`

Because if we divide 12 by any of these numbers, we’d have 0 as a remainder.

If I asked for the 5th factor, in this list, that would 6.

If I asked for the 8th factor in this list…well, there isn’t one. There are only 6 of them. So our function should return a -1. Let’s do this in code.

# Coding up a solution

Let’s define a function called kthFactor that takes in two arguments: n- the number we’re supposed to find factors of- and k, the nth factor we’re supposed to return. Next lets initialize an empty array to house our factors in:

`function kthFactor(n, k) {  let factors = [];}`

Now let’s find our factors. We said before that a factor of a number is that which when you divide by it, there is no remainder. To do that in code, we use the modulus operator. Let’s wrap this around a for loop in order to find all our factors in ascending order so that we’re able to return the nth item of a list. Next, let’s make a conditional so that every time we find a factor, we put it in our factors array.

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

Awesome. We’ve now found all our factors. How do we return the kth one though?

Note that we’re adding factors to our array in a linear fashion. In other words, as our for loop iterates, the factors list increments one number at a time. We actually don’t need to find all factors of our number. We just need to find the nth one that k is asking for. Thus, we can just return that number when our list reaches k size. And if we finish looping and don’t find it, we should return -1.

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

# Code Walk Through

Let’s walk through our code and note what’s happening at each step. If we called this function with n = 12, and k = 3:

• We begin looping at i = 1
• Does 12 / 1 have a remainder? Nope, so we push 1 into our factors array.
• Now our factors array is , which is of length 1. Since 1 is not 3, we skip the next conditional and increment i to 2.
• Does 12 / 2 have a remainder? Also no. So we push 2 into our factors array.
• Now our factors array is [1, 2], which is of length 2. Since 2 is not 3, we skip the next conditional and increment i to 3.
• Does 12 / 3 have a remainder? Also no. So we. push 3 into our factors array.
• Now our factors array is [1, 2, 3], which is of length 3. And now 3 = 3, so our next condition evaluates to true. So we return the number in our factors array at k — 1 (remember to account for the whole arrays starting at 0 index, but we started looping at i = 1 in order to return the nth element of a list), which is factors => 3.