1.9 KiB
1.9 KiB
id, challengeType, title, forumTopicId
id | challengeType | title | forumTopicId |
---|---|---|---|
5900f3941000cf542c50fea7 | 5 | Problem 40: Champernowne's constant | 302066 |
Description
Instructions
Tests
tests:
- text: <code>champernownesConstant(100)</code> should return 5.
testString: assert.strictEqual(champernownesConstant(100), 5);
- text: <code>champernownesConstant(1000)</code> should return 15.
testString: assert.strictEqual(champernownesConstant(1000), 15);
- text: <code>champernownesConstant(1000000)</code> should return 210.
testString: assert.strictEqual(champernownesConstant(1000000), 210);
Challenge Seed
function champernownesConstant(n) {
// Good luck!
return true;
}
champernownesConstant(100);
Solution
function champernownesConstant(n) {
let fractionalPart = '';
for (let i = 0; fractionalPart.length <= n; i++) {
fractionalPart += i.toString();
}
let product = 1;
for (let i = 0; i < n.toString().length; i++) {
const index = 10 ** i;
product *= parseInt(fractionalPart[index], 10);
}
return product;
}