Last active
January 3, 2017 18:54
-
-
Save pavel-lens/5743af82c60a2af40ec777735dde14a5 to your computer and use it in GitHub Desktop.
Imperative style implementation for CodeWars exercise https://www.codewars.com/kata/take-a-number-and-sum-its-digits-raised-to-the-consecutive-powers-and-dot-dot-dot-eureka/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sumDigPow(a, b) { | |
const arr = []; | |
for (let i = a; i <= b; i++) { | |
let sum = 0; | |
for (let j = 0; j <= String(i).length; j++) { | |
sum += Math.pow(parseInt(String(i)[j]), j+1); | |
if (sum == i) arr.push(i); | |
} | |
} | |
return arr; | |
} | |
console.log(sumDigPow(0, 150)); // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment