forked from 0xvashishth/CalcHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DivisorsCalc.js
34 lines (32 loc) · 1.16 KB
/
DivisorsCalc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function calc() {
if (document.getElementById("input").value != "") {
var n = document.getElementById("input").value;
var res = [];
if (n < 0) {
n *= -1;
}
// loop until n/i only to avoid repition so in each iteration we push i and n/i
for (var i = 1; i <= (n / i); i++) {
if (n % i == 0) {
res.push(i);
if ((i * i) != n)
res.push(n / i);; // to remove any duplicates
}
}
// Sorting result array
res.sort(function (a, b) {
return a - b
});
document.getElementById('dNb').innerHTML = res.length;
document.getElementById('d').innerHTML = res;
document.getElementById('dSum').innerHTML = res.reduce((a, b) => a + b, 0); // Calculating Divisors Sum
} else {
alert("Please input a number before clicking on calculate button");
}
}
function reset() {
document.getElementById("input").value = "";
document.getElementById('dNb').innerHTML = ".....";
document.getElementById('d').innerHTML = ".....";
document.getElementById('dSum').innerHTML = ".....";
}