This challenge involves using numbers up to 2**1000, so I decided to use BigInteger, but now I get this error: TypeError: Cannot convert a BigInt value to a number
When I try to run my code outside of FCC I don’t get an Error and my code gives the expected results. Does anyone know why this happens/how I could fix the error?
Your code so far
function powerDigitSum(exponent) {
let string=(2n**BigInt(exponent)).toString()
return string.split("").reduce((sum,d)=>sum+Number(d),0)
}
powerDigitSum(1000);
Your browser information:
User Agent is: Mozilla/5.0 (Linux; Android 8.1.0; SM-P580) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Safari/537.36.
Since the exponent itself never gets into BigInt territory you don’t need to cast it as a BigInt. And the number 2 is certainly not a BigInt so you don’t need to use 2n. What you can do is cast the result of 2**exponent as a BigInt and then it will work in these tests. I’m not saying your way is wrong, as it definitely works. But in order to pass the FCC test you’ll need to do it my way.