Dearest Corinthia, twas’t been a fortnite ‘til eyes lain upon. I yurn once more for to gaze upon your coding solution to a rando email. Ever yours, Dwayne Wyatt Evernly VI, Esq.
/* There is an N by M matrix of zeroes. Given N and M, write a function to count the number of ways of starting at the top-left corner and getting to the bottom-right corner. You can only move right or down.
For example, given a 2 by 2 matrix, you should return 2, since there are two ways to get to the bottom-right:
Right, then down Down, then right Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right. */
function countWays(N, M) { // create a zero'd out 2D array 'matrix' with dimensions N x M const matrix = Array.from({ length: N }, () => Array(M).fill(0)); matrix[0][0] = 1;
// fill the Matrix...*whoa* for (let i = 0; i < N; i++) { for (let j = 0; j < M; j++) { // If moving right is possible if (j > 0) { matrix[i][j] += matrix[i][j - 1]; } // If moving down is possible if (i > 0) { matrix[i][j] += matrix[i - 1][j]; } } }
// the bottom-right corner will have the number of ways to reach it return matrix[N - 1][M - 1]; }