--- title: Happy numbers id: 594810f028c0303b75339ad1 challengeType: 5 --- ## Description

A happy number is 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 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.

Implement a function that returns true if the number is happy, or false if not.

## Instructions
## Tests
```yml tests: - text: happy is a function. testString: 'assert(typeof happy === "function", "happy is a function.");' - text: happy(1) should return a boolean. testString: 'assert(typeof happy(1) === "boolean", "happy(1) should return a boolean.");' - text: happy(1) should return true. testString: 'assert(happy(1), "happy(1) should return true.");' - text: happy(2) should return false. testString: 'assert(!happy(2), "happy(2) should return false.");' - text: happy(7) should return true. testString: 'assert(happy(7), "happy(7) should return true.");' - text: happy(10) should return true. testString: 'assert(happy(10), "happy(10) should return true.");' - text: happy(13) should return true. testString: 'assert(happy(13), "happy(13) should return true.");' - text: happy(19) should return true. testString: 'assert(happy(19), "happy(19) should return true.");' - text: happy(23) should return true. testString: 'assert(happy(23), "happy(23) should return true.");' - text: happy(28) should return true. testString: 'assert(happy(28), "happy(28) should return true.");' - text: happy(31) should return true. testString: 'assert(happy(31), "happy(31) should return true.");' - text: 'happy(32) should return true:.' testString: 'assert(happy(32), "happy(32) should return true:.");' - text: happy(33) should return false. testString: 'assert(!happy(33), "happy(33) should return false.");' ```
## Challenge Seed
```js function happy (number) { // Good luck! } ```
## Solution
```js function happy (number) { let m; let digit; const cycle = []; while (number !== 1 && cycle[number] !== true) { cycle[number] = true; m = 0; while (number > 0) { digit = number % 10; m += Math.pow(digit, 2); number = (number - digit) / 10; } number = m; } return (number === 1); } ```