In Javascript: Math.max and Math.min do not work for BigInt types.
For example:
> Math.max(1n, 2n)
Thrown:
TypeError: Cannot convert a BigInt value to a number
at Math.max (<anonymous>)
>
Is there a built in function that performs these operations on BigInts?
how about
const bigIntMax = (...args) => args.reduce((m, e) => e > m ? e : m);
const bigIntMin = (...args) => args.reduce((m, e) => e < m ? e : m);
also if you want both then
const bigIntMinAndMax = (...args) => {
return args.reduce(([min,max], e) => {
return [
e < min ? e : min,
e > max ? e : max,
];
}, [args[0], args[0]]);
};
const [min, max] = bigIntMinAndMax(
BigInt(40),
BigInt(50),
BigInt(30),
BigInt(10),
BigInt(20),
);