2018-10-10 18:03:03 -04:00
---
id: 594810f028c0303b75339ad1
2021-02-06 04:42:36 +00:00
title: Happy numbers
2018-10-10 18:03:03 -04:00
challengeType: 5
2021-02-06 04:42:36 +00:00
forumTopicId: 302280
2021-01-13 03:31:00 +01:00
dashedName: happy-numbers
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
A [happy number ](https://en.wikipedia.org/wiki/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.
# --instructions--
Implement a function that returns true if the number is happy, or false if not.
2020-12-16 00:37:30 -07:00
# --hints--
2021-02-06 04:42:36 +00:00
`happy` should be a function.
2020-12-16 00:37:30 -07:00
```js
assert(typeof happy === 'function');
```
2021-02-06 04:42:36 +00:00
`happy(1)` should return a boolean.
2020-12-16 00:37:30 -07:00
```js
assert(typeof happy(1) === 'boolean');
```
2021-07-15 13:04:11 +05:30
`happy(1)` should return `true` .
2020-12-16 00:37:30 -07:00
```js
assert(happy(1));
```
2021-07-15 13:04:11 +05:30
`happy(2)` should return `false` .
2020-12-16 00:37:30 -07:00
```js
assert(!happy(2));
```
2021-07-15 13:04:11 +05:30
`happy(7)` should return `true` .
2020-12-16 00:37:30 -07:00
```js
assert(happy(7));
```
2021-07-15 13:04:11 +05:30
`happy(10)` should return `true` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(happy(10));
```
2021-07-15 13:04:11 +05:30
`happy(13)` should return `true` .
2020-12-16 00:37:30 -07:00
```js
assert(happy(13));
2018-10-10 18:03:03 -04:00
```
2021-07-15 13:04:11 +05:30
`happy(19)` should return `true` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(happy(19));
```
2018-10-10 18:03:03 -04:00
2021-07-15 13:04:11 +05:30
`happy(23)` should return `true` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(happy(23));
```
2018-10-10 18:03:03 -04:00
2021-07-15 13:04:11 +05:30
`happy(28)` should return `true` .
2020-12-16 00:37:30 -07:00
```js
assert(happy(28));
2018-10-10 18:03:03 -04:00
```
2021-07-15 13:04:11 +05:30
`happy(31)` should return `true` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(happy(31));
```
2018-10-10 18:03:03 -04:00
2021-07-15 13:04:11 +05:30
`happy(32)` should return `true` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(happy(32));
```
2018-10-10 18:03:03 -04:00
2021-07-15 13:04:11 +05:30
`happy(33)` should return `false` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(!happy(33));
2018-10-10 18:03:03 -04:00
```
2020-08-13 17:24:35 +02:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
function happy(number) {
}
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```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);
}
```