Skip to content

Instantly share code, notes, and snippets.

@sethschori
Last active July 31, 2016 19:03
Show Gist options
  • Save sethschori/96e3c54efc6f9676b2bd66628ff603ce to your computer and use it in GitHub Desktop.
Save sethschori/96e3c54efc6f9676b2bd66628ff603ce to your computer and use it in GitHub Desktop.
/*
Power Up!
Create a function powerUp() that takes an integer as input. It should return a new number, created by summing each digit raised by a consecutive power
ex)
powerUp(761) --> 7^1 + 6^2 + 1^3 === 44
powerUp(100) --> 1^1 + 0^2 + 0^3 === 1
*/
function powerUp(num) {
num = num.toString();
var sum = 0;
for (var i = 0; i < num.length; i++) {
sum += Math.pow(Number(num.charAt(i)),i+1);
}
return sum;
}
console.log(powerUp(761));
// 7^1 + 6^2 + 1^3 === 44
console.log(powerUp(100));
// 1^1 + 0^2 + 0^3 === 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment