Skip to content

Instantly share code, notes, and snippets.

@sghall
Last active January 23, 2017 19:20
Show Gist options
  • Save sghall/df3146a3290c1c40a3820ac154dcb820 to your computer and use it in GitHub Desktop.
Save sghall/df3146a3290c1c40a3820ac154dcb820 to your computer and use it in GitHub Desktop.
Count Factors of Number in JavaScript
function getFactorCount(num) {
if (num === 0) {
return;
}
if (num === 1) {
return 1;
}
let count = 2; // 1 and num;
const sqrt = Math.sqrt(num);
for (let i = 2; i < num; i++) {
if (num % i === 0) {
count++;
}
if (i >= sqrt && count === 2) { // number is prime
return 2;
}
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment