forked from 0xvashishth/CalcHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
48 lines (42 loc) · 1.54 KB
/
script.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// calculating factorial of the number
function factorial(x) {
if (x === 0 || x === 1) {
return 1n;
}
x = BigInt(x);
var cur = BigInt(x-1n);
while (cur>0n) {
x = x*cur;
cur = cur-1n;
}
console.log(x);
return x;
}
const calculate = () => {
var n = document.getElementById("n").value;
var r = document.getElementById("r").value;
// checking if values are number
if (n === "" || r === "" || isNaN(n) || isNaN(r)) {
console.log("jowijoif");
document.getElementById("alert").innerHTML = "Please provide correct values";
} else {
n = Number(n);
r = Number(r);
// if values are integers or not
if (!Number.isInteger(n) || !Number.isInteger(r)) {
document.getElementById("alert").innerHTML = "Please provide integer values";
// checking if values are positive or not
} else if (n < 0 || r < 0) {
document.getElementById("alert").innerHTML = "Please provide non negative values";
} else if (n < r) {
document.getElementById("alert").innerHTML = "n must be greater than or equal to r";
} else {
document.getElementById("alert").innerHTML = "";
let permutation = factorial(n)/factorial(n-r);
let combination = BigInt(permutation)/factorial(r);
console.log(permutation);
document.getElementById("per").innerHTML = permutation;
document.getElementById("comb").innerHTML = combination;
}
}
}