Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Update Happy_Number.js #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we'd need to check for 7 since it's already covered by n===1.

Suggested change
return (n === 1 || n === 7)? true:false;
return n === 1;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
Thanks for getting back to me, the thing is the first if condition checks whether the array's length is 1 or not,
in this case if the array has only 7 then it would be true, and since it is to return true if n===1 it would return false.

I have added some screenshots for tests please feel free to check them.

  • Not adding check for n===7
    Screenshot 2024-02-12 at 04-33-01 Happy Number - LeetCode

  • Adding checks
    Screenshot 2024-02-12 at 04-33-43 Happy Number - LeetCode

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhhh true! Nice catch. We need to list all happy numbers that are less than 10.

}
const digit = splitNumber.reduce((a,b)=> parseInt(a) + Math.pow(parseInt(b),2),0);
return checkHappyNumber(digit);
Expand Down