Chris Jones Chris Jones - 1 year ago 105
Javascript Question

Javascript Happy Numbers not working

Here I have a function that should take a number

into the
to check if all

n in [n-0
) are happy.

Happy Numbers wikipedia

If I only run
, I can tell that 7 is happy, but
doesn't show it. It is as if it doesn't receive the true. I have used
all over the place and happyChecker(n) shows a number that SHOULD return true. When I placed a
above the
return true;
, it showed that it branched into that branch but it just didn't seem to return the true.

function happyChecker(n) {
var arr = [];
var newNum = 0;
//here I split a number into a string then into an array of strings//
num = n.toString().split("");
for (var i = 0; i < num.length; i++) {
arr[i] = parseInt(num[i], 10);
//here I square each number then add it to newNum//
for (var i = 0; i < arr.length; i++) {
newNum += Math.pow(arr[i], 2);
//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) {
return false;

if (newNum === 1) {

return true;
} else {


function disHappy(num) {
for (j = num; j > 0; j--) {
if (happyChecker(j)) {
console.log(j + " is a Happy Number. It's so happy!!!.");

Answer Source

When you recurse, you need to return the value returned:

 if (newNum === 1) {

     return true;
 } else {
     return happyChecker(newNum);

You also should declare "num" with var.

I'm ordinarily not a "code golfer", but this is a good example of how the (new-ish) iterator utility methods on the Array prototype can clean up code. You can use the .reduce() function to traverse the array of digit characters and do the work of squaring and summing all at once:

var newNum = n.toString()
              .reduce(function(sum, digit) {
                return sum + (+digit * +digit);
               }, 0);

The call to .toString() returns a string, then .split('') gives you an array. Then .reduce() starts with an initial sum of 0 and for each element of the array (each digit), it adds to it the square of that digit. (Instead of parseInt() I just used the + unary operator; we know for sure that each string will be a valid number and an integer.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download