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);
for (let i=0;i<digits.length;i++){
    let power = 0;    
    let sum = 0;
    power = Math.pow(digits[i],digits.length);
    sum += power;

Can someone please help me with this?!

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


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.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.