Last active
November 22, 2017 22:10
-
-
Save vedovelli/9f85f323c8044f84132082377e2cde9f to your computer and use it in GitHub Desktop.
Happy number resolution
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
// http://dojopuzzles.com/problemas/exibe/numeros-felizes/ | |
const numberToAssert = 7 | |
//************************ | |
let iterationLimit = 200 | |
const assertHappiness = input => { | |
const numbersArray = input.toString().split('') | |
const squaredArray = numbersArray.map(number => number * number) | |
const result = squaredArray.reduce((acc, item) => acc + item, 0) | |
const isInputHappy = parseInt(result, 10) === 1 | |
if (iterationLimit === 0) { | |
return `${numberToAssert} is a sad number` | |
} | |
if (isInputHappy) { | |
return `${numberToAssert} is a happy number` | |
} | |
iterationLimit-- | |
return assertHappiness(result) | |
} | |
console.log( | |
assertHappiness(numberToAssert) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment