2018-09-30 23:01:58 +01:00
---
id: 5900f37b1000cf542c50fe8e
challengeType: 5
title: 'Problem 15: Lattice paths'
2019-08-05 09:17:33 -07:00
forumTopicId: 301780
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
2020-02-28 21:39:47 +09:00
2018-09-30 23:01:58 +01:00
Starting in the top left corner of a 2× 2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.
2020-02-28 21:39:47 +09:00
< img class = "img-responsive center-block" alt = "a diagram of 6 2 by 2 grids showing all the routes to the bottom right corner" src = "https://cdn-media-1.freecodecamp.org/project-euler/1Atixoj.gif" style = "background-color: white; padding: 10px;" >
How many such routes are there through a given `gridSize` ?
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2020-02-28 21:39:47 +09:00
- text: < code > latticePaths(4)</ code > should return a number.
testString: assert(typeof latticePaths(4) === 'number');
2018-10-04 14:37:37 +01:00
- text: < code > latticePaths(4)</ code > should return 70.
2019-07-25 08:44:15 -07:00
testString: assert.strictEqual(latticePaths(4), 70);
2018-10-04 14:37:37 +01:00
- text: < code > latticePaths(9)</ code > should return 48620.
2019-07-25 08:44:15 -07:00
testString: assert.strictEqual(latticePaths(9), 48620);
2018-10-04 14:37:37 +01:00
- text: < code > latticePaths(20)</ code > should return 137846528820.
2019-07-25 08:44:15 -07:00
testString: assert.strictEqual(latticePaths(20), 137846528820);
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function latticePaths(gridSize) {
2020-09-15 09:57:40 -07:00
2018-09-30 23:01:58 +01:00
return true;
}
latticePaths(4);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function latticePaths(gridSize) {
let paths = 1;
for (let i = 0; i < gridSize ; i + + ) {
paths *= (2 * gridSize) - i;
paths /= i + 1;
}
return paths;
}
```
< / section >