Please debug the following trenchTranveral javascript function. The function should return true if the node's value is less than -5 and false if equal to or greater than -5. function findNeighbors(node, matrix) { const neighbors = []; const [row, col] = node; const currNum = matrix[row][col]; // Check top if (matrix[row - 1] && Math.abs(matrix[row - 1][col] < - 5)) { neighbors.push([row - 1, col]); } // Check bottom if (matrix[row + 1] && Math.abs(matrix[row + 1][col] < - 5)) { neighbors.push([row + 1, col]); } // Check left if (matrix[row][col - 1] && Math.abs(matrix[row][col - 1] < - 5)) { neighbors.push([row, col - 1]); } // Check right if (matrix[row][col + 1] && Math.abs(matrix[row][col + 1] < - 5)) { neighbors.push([row, col + 1]); } // Return neighbors return neighbors; // Only consider N, S, E, W nodes } function trenchTraversal(node, matrix, visited) { const [row, col] = node; let stack = [node] visited.add(node.toString()) while(stack.length){ const [row, col] = stack.pop() const neighbors = findNeighbors([row, col], matrix) if (matrix[row][col] < - 5) return true; neighbors.forEach((newNode) => { if(!visited.has(newNode.toString())){ stack.push(newNode) visited.add(newNode.toString()) } }) } return false; } examples for testing: const sonar_1 = [ [-5, -5, -5, -5, -5], [-5, -8, -8, -9, -7], [-5, -5, -5, -5, -8], [-5, -5, -5, -5, -5] ]; let visited1 = new Set() console.log(trenchTraversal([2, 3], sonar_1, visited1)) // should return false // let visited2 = new Set(); // console.log(trenchTraversal([1, 1], sonar_1, visited2)) //should return true
Please debug the following trenchTranveral javascript function. The function should return true if the node's value is less than -5 and false if equal to or greater than -5.
function findNeighbors(node, matrix) { const neighbors = []; const [row, col] = node; const currNum = matrix[row][col]; // Check top if (matrix[row - 1] && Math.abs(matrix[row - 1][col] < - 5)) { neighbors.push([row - 1, col]); } // Check bottom if (matrix[row + 1] && Math.abs(matrix[row + 1][col] < - 5)) { neighbors.push([row + 1, col]); } // Check left if (matrix[row][col - 1] && Math.abs(matrix[row][col - 1] < - 5)) { neighbors.push([row, col - 1]); } // Check right if (matrix[row][col + 1] && Math.abs(matrix[row][col + 1] < - 5)) { neighbors.push([row, col + 1]); } // Return neighbors return neighbors; // Only consider N, S, E, W nodes } function trenchTraversal(node, matrix, visited) { const [row, col] = node; let stack = [node] visited.add(node.toString()) while(stack.length){ const [row, col] = stack.pop() const neighbors = findNeighbors([row, col], matrix) if (matrix[row][col] < - 5) return true; neighbors.forEach((newNode) => { if(!visited.has(newNode.toString())){ stack.push(newNode) visited.add(newNode.toString()) } }) } return false; }
examples for testing:
const sonar_1 = [
[-5, -5, -5, -5, -5],
[-5, -8, -8, -9, -7],
[-5, -5, -5, -5, -8],
[-5, -5, -5, -5, -5]
];
let visited1 = new Set()
console.log(trenchTraversal([2, 3], sonar_1, visited1)) // should return false
// let visited2 = new Set();
// console.log(trenchTraversal([1, 1], sonar_1, visited2)) //should return true
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 1 images