Find paths
1. Find the nearest store:
var x = [[1,7],[2,3],[4,5]] var r = []; x.forEach((coor) => r.push(coor[0] * coor[0] + coor[1] * coor[1])) console.log(x[r.indexOf(Math.min(...r))]) >> [2, 3]
2. Find all possible ways from home [0,0] to 9. Where 9 is the end of the path. 1 is a possible way. Count steps for each possible way.
var x = [ [1,0,0,0,0,1], [1,1,1,0,0,1], [1,0,1,0,0,1], [1,1,1,1,1,9], [1,0,0,0,0,0] ] var _9 = [0,0] x.forEach((row, i) => row.forEach((value, j) => { if (value === 9) { _9 = [i,j] return } })) var findPath = (i,j,cost) => { if (i < 0 || j < 0 || x[i,j] === 0) return if (i === 0 && j === 0) return console.log(cost) ++cost if (i > 0 && x[i-1][j] === 1) findPath(i-1,j,cost) if (j > 0 && x[i][j-1] === 1) findPath(i,j-1,cost) } findPath(..._9,0) >> 8 >> 8