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

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 19RQ
icon
Related questions
Question

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

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 1 images

Blurred answer
Knowledge Booster
Matrix multiplication
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage