I'm trying to do this problem where you are given an array of arrays. You start at the first item and them move either 1 item to the right or 1 item down depending on which item is bigger. the goal is to make it to the bottom right corner with the highest sum possible. Maybe I'm missing the point, but I figured this was a recursive function.
let map = [
[8,3,5],
[4,3,4],
[2,2,3]
]
const find = (y,x,map) => {
if (y === map.length - 1 && x === map[map.length - 1].length - 1){
return map[y][x]
} else if(map[y + 1][x] > map[y][x + 1]){
return map[y][x] + find(((y + 1), x,map))
} else {
return map[y][x] + find((y,(x + 1),map))
}
}
console.log(find(0,0,map))
In this case the goal is to get 22 via 8->4->3->4->3, but whenever I pass the map into the next level of recursion, the array on the next level reads as undefined. Is there any way to pass down the array of arrays so that it can be read by other levels of the recursive function?