Why doesn't sum increment in this case?

Hi everyone!

I am learning Javascript and I have this problem to raise the digits of the given number to the number of digits of the given number (ex 153 = 1^3+5^3+3^3). ^ should be **
I can’t figure out why sum does not increment with power, the times the number of digits the given number has.

let num = 153;
let digits = Array.from(String(num),Number);
console.log(digits);
for (let i=0;i<digits.length;i++){
    let power = 0;    
    let sum = 0;
    power = Math.pow(digits[i],digits.length);
    sum += power;
    console.log(sum);
} 

Can someone please help me with this?!

sum starts from 0 at each iteration of the loop, so it will never increase

4 Likes

The n how do i do it? I am sitting here for hours and i feel frustrated and stupid.

Edit: I have make the addition the times the digits, if there are 3 digits in the number then I have add the power of the number 3 times. How can I do that?

Your sum is only inside of the loop. If you want the value to exist for every loop iteration, then it needs to be outside of the loop.

3 Likes