My Javascript Solution


  • 0
    R
    var isHappy = function(num) {
        var history = [];
        var squared;
        
        while(history.indexOf(num) < 0){
            history.push(num);
            num = num.toString().split('');
            squared = num.reduce(function(total, number){return (parseInt(number) * parseInt(number)) + total}, 0);
            if(squared === 1){
                return true;
            }else{
                num = squared;
            }
        }
    
        return false;
    };

Log in to reply
 

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