I am trying to create a simple function that returns the sum of all the digits in a number (after power operation). The thing is, I am getting a weird result when working with really big numbers that use scientific notation.
I assume it's because of something with the Math.floor() method. But I did try to read on the docs if there is a problem using this method with big numbers and I didn't see such a warning.
My code looks like that:
function fixedPowerToString(num, pow) {
let powResult = Math.pow(num, pow);
let sum = 0;
let result = powResult;
while(result >= 10) {
sum += result % 10;
result = Math.floor(result / 10);
}
sum += result;
return sum;
}
console.log(fixedPowerToString(2, 1000)); // returns 1189 instead of 1366
when testing with smaller numbers it seems to be fine. So I assume it's because the number is too big to process correctly?