2018-10-04 14:37:37 +01:00
---
title: Happy numbers
id: 594810f028c0303b75339ad1
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302280
2018-10-04 14:37:37 +01:00
---
## Description
< section id = 'description' >
2019-03-05 18:37:06 +09:00
A < a href = "https://en.wikipedia.org/wiki/Happy_number" target = "_blank" > happy number< / a > is defined by the following process:
2019-06-14 20:04:16 +09:00
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 < code > 1< / code > (where it will stay), or it loops endlessly in a cycle which does not include < code > 1< / code > . Those numbers for which this process ends in < code > 1< / code > are happy numbers, while those that do not end in < code > 1< / code > are unhappy numbers.
2018-10-04 14:37:37 +01:00
< / section >
## Instructions
< section id = 'instructions' >
2019-03-05 18:37:06 +09:00
Implement a function that returns true if the number is happy, or false if not.
2018-10-04 14:37:37 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2019-11-20 07:01:31 -08:00
- text: < code > happy</ code > should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof happy === 'function');
2018-10-04 14:37:37 +01:00
- text: < code > happy(1)</ code > should return a boolean.
2019-07-26 05:24:52 -07:00
testString: assert(typeof happy(1) === 'boolean');
2018-10-04 14:37:37 +01:00
- text: < code > happy(1)</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(happy(1));
2018-10-04 14:37:37 +01:00
- text: < code > happy(2)</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!happy(2));
2018-10-04 14:37:37 +01:00
- text: < code > happy(7)</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(happy(7));
2018-10-04 14:37:37 +01:00
- text: < code > happy(10)</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(happy(10));
2018-10-04 14:37:37 +01:00
- text: < code > happy(13)</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(happy(13));
2018-10-04 14:37:37 +01:00
- text: < code > happy(19)</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(happy(19));
2018-10-04 14:37:37 +01:00
- text: < code > happy(23)</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(happy(23));
2018-10-04 14:37:37 +01:00
- text: < code > happy(28)</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(happy(28));
2018-10-04 14:37:37 +01:00
- text: < code > happy(31)</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(happy(31));
2018-10-20 21:02:47 +03:00
- text: < code > happy(32)</ code > should return true:.
2019-07-26 05:24:52 -07:00
testString: assert(happy(32));
2018-10-04 14:37:37 +01:00
- text: < code > happy(33)</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!happy(33));
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-03-05 18:37:06 +09:00
function happy(number) {
2020-09-15 09:57:40 -07:00
2018-10-04 14:37:37 +01:00
}
```
< / div >
< / section >
## Solution
< section id = '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);
}
```
< / section >