You’ve got the right idea, but you aren’t being aggressive enough. n/2 is roughly as bad as n. But the biggest factor, other than n, is much smaller. You have to be careful about how you count, but you only need to check sqrt(n) values to count the factors.

function numberOfFactors(n) {
let count=2
let i=2
while(i**2<n) {
if (n % i == 0)
// Because factors come in pairs
count += 2
i += 1
}
if (i**2==n)
count++
return count
}

function numberOfFactors(n) {
let count=2
let i=2
// Single bound computation is faster
const bound = math.floor(math.sqrt(n))
// Check i from 2 to largest possible factor
while(i < bound) {
if (n % i == 0)
// Because factors come in pairs
// if i is a factor, n/i is too
count += 2
i += 1
}
// If n = i*i, then we only want to count
// the factor i once
if (i**2==n)
count++
return count
}

I added a couple of comments that maybe help explain things?