Tell us what’s happening:
Cannot figure out what I am doing wrong here. Can anyone help?
3. powerDigitSum(128) should return 166.
4. powerDigitSum(1000) should return 1366.
// tests completed
// console output
2^15 = 32768
Sum of digits = 26
2^15 = 32768
Sum of digits = 26
Your code so far
function powerDigitSum(exponent) {
const number = Math.pow(2, exponent); // regular number
const digitSum = number
.toString()
.split('')
.reduce((sum, digit) => sum + Number(digit), 0);
console.log('2^' + exponent + ' = ' + number);
console.log('Sum of digits = ' + digitSum);
return digitSum;
}
// Works fine for small exponents:
powerDigitSum(15); // ✅ 2^15 = 32768, Sum = 26
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0
Challenge Information:
Project Euler Problems 1 to 100 - Problem 16: Power digit sum