Created
December 2, 2018 08:57
-
-
Save EricHech/ec9f068f542e1fc6342c0d4a44c44d8c to your computer and use it in GitHub Desktop.
An answer to a version of the "find the number of paths" CC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If a square is divided diagonally so that you have an isosceles right triangle, | |
// this function will find how many paths may be taken to get from one corner to the given index | |
function numberOfPathsInDiagonalGrid(x, y) { | |
if (x === 1 || y === 1) return 1; | |
if (x > y) return numberOfPathsInDiagonalGrid(x - 1, y); | |
else return ( | |
numberOfPathsInDiagonalGrid(x - 1, y) + | |
numberOfPathsInDiagonalGrid(x, y - 1) | |
); | |
} | |
console.log(numberOfPathsInDiagonalGrid(3, 3)); | |
/* | |
0,0 > 0,1 > 0,2 > 0,3 > 1,3 > 2,3 > 3,3 | |
0,0 > 0,1 > 0,2 > 1,2 > 1,3 > 2,3 > 3,3 | |
0,0 > 0,1 > 0,2 > 1,2 > 2,2 > 2,3 > 3,3 | |
0,0 > 0,1 > 1,2 > 1,2 > 2,2 > 2,3 > 3,3 | |
0,0 > 0,1 > 1,1 > 1,2 > 1,3 > 2,3 > 3,3 | |
0,0 - 1,1 - 2,2 - 2,2 - 3,3 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment