Skip to content

Instantly share code, notes, and snippets.

@javi-aire
Last active April 3, 2020 06:47
Show Gist options
  • Select an option

  • Save javi-aire/ff5349fefa0dd9d7eb50405b4f0ef4fe to your computer and use it in GitHub Desktop.

Select an option

Save javi-aire/ff5349fefa0dd9d7eb50405b4f0ef4fe to your computer and use it in GitHub Desktop.
Problem 2/30 of LeetCode 30-day challenge
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