Skip to content

Find if a number is a happy number in JavaScript


The happy number can be defined as a number which will yield 1 when it is replaced by the sum of the square of its digits repeatedly. If this process results in an endless cycle of numbers containing 4, then the number is called an unhappy number.

const sqSum = (num, result = 0) => {
      return sqSum(Math.floor(num/10), result+Math.pow((num%10),2));
   return result;
const isHappy = (num, map = {}) => {
   if(num !== 1){
         return false;
      map[num] = 1;
      return isHappy(sqSum(num), map);
   return true;

console.log(isHappy(82)); //true
See also  Fibonacci Number - Leetcode Challenge - Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.