Files
freeCodeCamp/curriculum/challenges/english/10-coding-interview-prep/rosetta-code/happy-numbers.md
Oliver Eyton-Williams ee1e8abd87 feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

130 lines
2.0 KiB
Markdown

---
id: 594810f028c0303b75339ad1
title: Happy numbers
challengeType: 5
forumTopicId: 302280
dashedName: happy-numbers
---
# --description--
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.
# --hints--
`happy` should be a function.
```js
assert(typeof happy === 'function');
```
`happy(1)` should return a boolean.
```js
assert(typeof happy(1) === 'boolean');
```
`happy(1)` should return true.
```js
assert(happy(1));
```
`happy(2)` should return false.
```js
assert(!happy(2));
```
`happy(7)` should return true.
```js
assert(happy(7));
```
`happy(10)` should return true.
```js
assert(happy(10));
```
`happy(13)` should return true.
```js
assert(happy(13));
```
`happy(19)` should return true.
```js
assert(happy(19));
```
`happy(23)` should return true.
```js
assert(happy(23));
```
`happy(28)` should return true.
```js
assert(happy(28));
```
`happy(31)` should return true.
```js
assert(happy(31));
```
`happy(32)` should return true:.
```js
assert(happy(32));
```
`happy(33)` should return false.
```js
assert(!happy(33));
```
# --seed--
## --seed-contents--
```js
function happy(number) {
}
```
# --solutions--
```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);
}
```