Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-40-champernownes-constant.english.md
Kristofer Koishigawa 6cfd0fc503 fix: improve Project Euler descriptions, challenge seeds, and test cases (#38016)
* fix: improve Project Euler descriptions and test case

Improve formatting of Project Euler test descriptions. Also add poker hands array and new test case for problem 54

* feat: add typeof tests and gave functions proper names for first 100 challenges

* fix: continue fixing test descriptions and adding "before test" sections

* fix: address review comments

* fix: adjust grids in 18 and 67 and fix some text that reference files rather than the given arrays

* fix: implement bug fixes and improvements from review

* fix: remove console.log statements from seed and solution
2020-02-28 06:39:47 -06:00

2.0 KiB
Raw Blame History

id, challengeType, title, forumTopicId
id challengeType title forumTopicId
5900f3941000cf542c50fea7 5 Problem 40: Champernowne's constant 302066

Description

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021...

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fractional part, find the value of the following expression.

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

Instructions

Tests

tests:
  - text: <code>champernownesConstant(100)</code> should return a number.
    testString: assert(typeof champernownesConstant(100) === 'number');
  - 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;
}