Skip to content

Commit

Permalink
Update Happy_Number.js (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignacio-chiazzo authored Feb 12, 2024
2 parents 21e198d + 9e1d57d commit 2b375b0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion LeetcodeProblems/Algorithms/easy/Happy_Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ Example 2:
Input: n = 2
Output: false
Example 3:
Input n = 7
Output = true
Explanation:
7^2 = 49
4^2 + 9^2 = 97
9^2 + 7^2 = 130
1^2 + 3^2 + 0^2 = 10
1^2 + 0^2 = 1
*/

Expand All @@ -40,7 +49,7 @@ function checkHappyNumber(n){
let strNumber = n.toString();
let splitNumber = strNumber.split("");
if(splitNumber.length <= 1){
return (n <= 1)? true:false;
return (n === 1 || n === 7)? true:false;
}
const digit = splitNumber.reduce((a,b)=> parseInt(a) + Math.pow(parseInt(b),2),0);
return checkHappyNumber(digit);
Expand Down

0 comments on commit 2b375b0

Please sign in to comment.