This problem has still been nagging at me for weeks, and I’ve made no further progress. I’ve got previous code passing tests, and here’s that which is still giving me trouble.
// Function to traverse trenches
function trenchTraversal(node, matrix, visited) {
// Get coordinates of node passed in
const [row, col] = node;
// Log visited set
console.log(`Visited Set: ${visited}`);
// If node depth less than -5, return false
if (matrix[row][col] >= -5) return false;
// Set stack equal to node passed in
const stack = [node];
// Add node passed in to visited set
visited.add(node.toString());
// Path array
const pathArr = [];
// While the stack has length
while (stack.length) {
// Get current node from stack
const current = stack.pop();
// Push current node to path array
pathArr.push(current);
// Set current coordinates to the current node
const [currentRow, currentCol] = current;
// Get current node value from matrix
const currentNode = matrix[currentRow][currentCol];
// Find neighbors of current node
const neighbors = findNeighbors(current, matrix);
const neighborCoords = [];
neighbors.forEach(neighbor => {
neighborCoords.push([neighbor[0], neighbor[1]].toString());
});
// Loop through neighbors
neighborCoords.forEach(neighborCoord => {
// If the visited note doesn't have neighbor coordinates
if (!visited.has(neighborCoord)) {
// Push the neighbor onto the stack
stack.push(neighbors[neighborCoord]);
// Add neighbor coordinates to visisted set
visited.add(neighborCoord.toString());
};
// Check if trench is T-shaped
if (visited.has(neighborCoord[0] - 1 && neighborCoord[1] - 1)) return false;
});
// If the path is three or more nodes long, return true
if (pathArr.length >= 3) return true;
};
// Return false
return false;
};
// Function to identify trenches
function identifyTrench(trenchMatrix) {
// Create set to hold visited coordinates
const visited = new Set();
// Loop for rows
for (let i = 0; i < trenchMatrix.length; i++) {
// Inner loop for columns
for (let j = 0; j < trenchMatrix[0].length; j++) {
// If the coordinate is deeper than -5
if (trenchMatrix[i][j] < -5) {
// If a valid trench exists, return true
if (trenchTraversal([i, j], trenchMatrix, visited)) return true;
// Else return false
else return false;
};
};
};
// Return false if no valid trenches are found
return false;
};
And I get this error.
TypeError: current is not iterable
I remember not getting this error before when I was last working on the code. I would appreciate any help in getting this problem completely resolved.