Last active
January 3, 2017 19:33
-
-
Save pavel-lens/a032a7508fed3fe33eeed21a8cb4971a to your computer and use it in GitHub Desktop.
Declarative 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(start, stop) { | |
// create array from start to stop | |
const arr = Array.apply(null, Array(stop-start)).map((_,i) => i+start); | |
function calc(num) { | |
const n_arr = String(num).split('').map(Number).map((n,i) => Math.pow(n,i+1)); | |
return n_arr.reduce((acc,n) => acc+n); | |
} | |
// create new array with numbers that match criteria | |
return arr.filter((n) => calc(n) === n ? true : false); | |
} | |
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