Find Happy Number Using JS

What is happy number?

A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum
of the squares of its digits, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers). DEMO

function isHappyChecker(n) {
 		var arr = [];
    var newNum = 0;
  	var arr = n.toString().split("");
    for (var i = 0; i < arr.length; i++) {
         newNum += Math.pow(arr[i], 2);
     }
     if (newNum === 1) {
				alert("it is a happy number");
         return true;
     } else {
     //here I noticed that all unhappy numbers eventually came into one of these three//
     //( and more) numbers, so I chose them to shorten the checking. A temporary solution for sure//       
         if (newNum === 58 || newNum === 4 || newNum == 37) {
        	   alert("it is a NOT happy number");
             return false;
         }
         return isHappyChecker(newNum);
     }
  }
Advertisements

Published by

shiyam

My passion became my profession , I'm a driven, energetic and proactive tech professionally. I consider myself an engagement professional, as I don't simply provide solutions, I create them. I am a continuous learner – always looking for new technologies, in my spare time I search Google for the latest trends . I like to think of myself as a futurist and I'm a Full Stack JavaScript Developer. >> Skills << ✔ Web App Development ✔ Strong Javascript ✔ Backbone.js, Angular.js, Dojo.js ✔ jQuery.js ✔ Node.js ✔ MongoDb , MySql ✔ Socket.io / Web Sockets (Native) ✔ PHP ✔ Ruby (Watir - Automation) ✔ Grunt ✔ Github , Bitbucket , SVN ✔ Agile Process ✔ Scrum ✔ Ability to research and adapt new techs ✔ Fast Learner ✔ Always Punctual ✔ Critical Thinker ✔ Friendly Personality

One thought on “Find Happy Number Using JS”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s