No magic JS solution


  • 0
    K
    var isHappy = function(n) {
        var map = new Set();
        while(n !== 1){
            n = squareSum(n);
            if(map.has(n)) return false
            map.add(n);
        }
        return true;
    };
    
    var squareSum = function(num){
      var sum = 0;
      var str = num+"";
      for(var i=0; i<str.length; i++)
          sum += Math.pow(parseInt(str.charAt(i)), 2);
        return sum;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.