1.9 KiB
1.9 KiB
title | id | challengeType | forumTopicId |
---|---|---|---|
Iterated digits squaring | 5a23c84252665b21eecc7ec1 | 5 | 302291 |
Description
15 -> 26 -> 40 -> 16 -> 37 -> 58 -> 89 7 -> 49 -> 97 -> 130 -> 10 -> 1
Instructions
Tests
tests:
- text: <code>iteratedSquare</code> should be a function.
testString: assert(typeof iteratedSquare=='function');
- text: <code>iteratedSquare(4)</code> should return a number.
testString: assert(typeof iteratedSquare(4)=='number');
- text: <code>iteratedSquare(4)</code> should return <code>89</code>.
testString: assert.equal(iteratedSquare(4),89);
- text: <code>iteratedSquare(7)</code> should return <code>1</code>.
testString: assert.equal(iteratedSquare(7),1);
- text: <code>iteratedSquare(15)</code> should return <code>89</code>.
testString: assert.equal(iteratedSquare(15),89);
- text: <code>iteratedSquare(20)</code> should return <code>89</code>.
testString: assert.equal(iteratedSquare(20),89);
- text: <code>iteratedSquare(70)</code> should return <code>1</code>.
testString: assert.equal(iteratedSquare(70),1);
- text: <code>iteratedSquare(100)</code> should return <code>1</code>.
testString: assert.equal(iteratedSquare(100),1);
Challenge Seed
function iteratedSquare(n) {
// Good luck!
}
Solution
function iteratedSquare(n) {
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;
}