this is the tabulated version of gridTraveler, where you output the number of ways one can travel from the top left to the bottom right corner in an m*n box, but you can only move down or right.
const gridTraveler = (m, n) => {
const table = Array(m+1)
.fill()
.map(_ => Array(n+1).fill(0));
table[1][1] = 1;
for(let i=0; i<=m; i++){
console.log(i + 'th row');
for(let j=0; j<=n; j++){
if (i===0 || j===0){
//do nothing
}else{
table[i][j] = table[i-1][j] + table[i][j-1];
}
console.log(table[i][j]);
}
}
return table[m][n];
}
console.log(gridTraveler(2, 3));
I’ve tested most parts of the code and they all work as intended, except for the line where i assign a value to table[i][j]. Forr some reason the for loop keeps making all elements 0, even though it should give the correct answer on paper. i’ve tried asking gpt and it says my code is correct. Anyone know what’s wrong?