I am trying to use BigInt() to compute the factorial of number greater than 20.
Below is my function to compute the factorial:
function extraLongFactorials(n) {
// let result=BigInt(1);
// console.log(typeof(result));
let result=BigInt(1);
for(let i=n;i>1;i--){
result*=BigInt(i);
}
// console.log(typeof(result));
// console.log(result);
// BigInt.prototype.toJSON = function() { return this.toString() }
// result=JSON.stringify(result)
console.log(result);
}
I am getting the result, however in the end n is being appended, so the result is something like 15511210043330985984000000n, whereas I am expecting to get the result 15511210043330985984000000. Is there a way to remove the n from the end ?
Use toString
:
console.log(result.toString());
// or
console.log(String(result));
// or
console.log('' + result);