2018-10-04 13:37:37 +00:00
|
|
|
---
|
|
|
|
title: Iterated digits squaring
|
|
|
|
id: 5a23c84252665b21eecc7ec1
|
|
|
|
challengeType: 5
|
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
|
|
|
If you add the square of the digits of a Natural number (an integer bigger than zero), you always end with either 1 or 89:
|
2019-03-06 05:18:18 +00:00
|
|
|
<pre>
|
|
|
|
15 -> 26 -> 40 -> 16 -> 37 -> 58 -> 89
|
|
|
|
7 -> 49 -> 97 -> 130 -> 10 -> 1
|
|
|
|
</pre>
|
2018-10-04 13:37:37 +00:00
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
2019-03-06 05:18:18 +00:00
|
|
|
Write a function that takes a number as a parameter and returns 1 or 89 after performing the mentioned process.
|
2018-10-04 13:37:37 +00:00
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>iteratedSquare</code> should be a function.
|
2019-07-26 12:24:52 +00:00
|
|
|
testString: assert(typeof iteratedSquare=='function');
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>iteratedSquare(4)</code> should return a number.
|
2019-07-26 12:24:52 +00:00
|
|
|
testString: assert(typeof iteratedSquare(4)=='number');
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>iteratedSquare(4)</code> should return <code>89</code>.
|
2019-07-26 12:24:52 +00:00
|
|
|
testString: assert.equal(iteratedSquare(4),89);
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>iteratedSquare(7)</code> should return <code>1</code>.
|
2019-07-26 12:24:52 +00:00
|
|
|
testString: assert.equal(iteratedSquare(7),1);
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>iteratedSquare(15)</code> should return <code>89</code>.
|
2019-07-26 12:24:52 +00:00
|
|
|
testString: assert.equal(iteratedSquare(15),89);
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>iteratedSquare(20)</code> should return <code>89</code>.
|
2019-07-26 12:24:52 +00:00
|
|
|
testString: assert.equal(iteratedSquare(20),89);
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>iteratedSquare(70)</code> should return <code>1</code>.
|
2019-07-26 12:24:52 +00:00
|
|
|
testString: assert.equal(iteratedSquare(70),1);
|
2018-10-08 11:52:10 +00:00
|
|
|
- text: <code>iteratedSquare(100)</code> should return <code>1</code>.
|
2019-07-26 12:24:52 +00:00
|
|
|
testString: assert.equal(iteratedSquare(100),1);
|
2018-10-04 13:37:37 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
2019-03-06 05:18:18 +00:00
|
|
|
function iteratedSquare(n) {
|
2018-10-04 13:37:37 +00:00
|
|
|
// Good luck!
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
2019-03-06 05:18:18 +00:00
|
|
|
function iteratedSquare(n) {
|
2018-10-04 13:37:37 +00:00
|
|
|
var total;
|
|
|
|
while (n != 89 && n != 1) {
|
|
|
|
total = 0;
|
|
|
|
while (n > 0) {
|
|
|
|
total += Math.pow(n % 10, 2);
|
|
|
|
n = Math.floor(n/10);
|
|
|
|
}
|
|
|
|
n = total;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|