Last active
April 3, 2020 06:47
-
-
Save javi-aire/ff5349fefa0dd9d7eb50405b4f0ef4fe to your computer and use it in GitHub Desktop.
Problem 2/30 of LeetCode 30-day challenge
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
| let isHappy = (inputNum) => { | |
| // keep track of permutations | |
| let unhappyNums = []; | |
| let newSum = inputNum; | |
| // while the inputNum is not in the array | |
| while(!unhappyNums.includes(newSum)) { | |
| unhappyNums.push(newSum); | |
| // calculate new sum by converting to str, | |
| // split by character then add the sum of each squared digit | |
| newSum = newSum.toString().split('').reduce((acc, num) => { | |
| return acc + (parseInt(num) ** 2); | |
| }, 0); | |
| if(newSum === 1){ | |
| // if that sum is 1 return true | |
| return true; | |
| }; | |
| } | |
| // if inputNum is found in array, | |
| // while loop will not start | |
| // return false | |
| return false; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment