Question
Can You fix my javascript code to pass all specs: ASSIGNMENT INFORMATION: You have been assigned to work with an undersea explorer who is attempting
Can You fix my javascript code to pass all specs:
ASSIGNMENT INFORMATION:
You have been assigned to work with an undersea explorer who is attempting to identify and map undersea trenches. The explorer has provided you with several data sets of sonar readings in this format:
// Example 1 sonar = [ [-5,-5,-5,-5,-5], [-5,-8,-8,-9,-7], [-5,-5,-5,-5,-8], [-5,-5,-5,-5,-5] ]
Depending on the scan, the provided matrix may be larger or smaller, but it will always be rectangular. Your task is to determine if a given data set contains a trench by comparing each node and their neighbors and determining if there is a pattern that matches the defined properties of a trench.
Neighbors are considered to be nodes that are directly above, below, or to the side. No diagonals!
A trench has the following three properties:
It has a length of three or more nodes that are neighbors.
Each node in the trench must be deeper than -5.
Trenches may not branch into (any form of) a "T" shape. A node with more than two neighbors will result in branching "T" shape.
This problem literally has edge cases! After discussing the matter with the explorer, you have agreed that potential trenches that otherwise meet the rules as a trench are valid, even if part of it is on the edge of the data set, and it can't be fully determined if those nodes follow the rules.
A few examples:
// Example 1 sonar_1 = [ [-5,-5,-5,-5,-5], [-5,-8,-8,-9,-7], [-5,-5,-5,-5,-8], [-5,-5,-5,-5,-5] ]
Example 1 has a trench. The nodes containing -8, -8, -9, -7, -8 meet the rules. This is an edge case trench, because the nodes containing -7 and -8 are on the edge of the data set.
// Example 2 sonar_2 = [ [-5,-5,-5,-7,-5], [-5,-8,-8,-9,-5], [-5,-5,-5,-7,-5], [-5,-5,-5,-5,-5] ]
Example 2 does not have a trench. The node containing -9 has three neighbors with a depth below -5. This would create a "T" shaped trench, which is not valid.
// Example 3 sonar_3 = [ [-5,-5,-5,-5,-5], [-5,-5,-5,-5,-5], [-5,-9,-9,-5,-5], [-5,-5,-5,-5,-5] ]
Example 3 does not have a trench. There are two nodes that are next to one another and are deeper than -5, but a trench must have at least 3 total nodes to be a trench.
function findNeighbors(node, matrix) {
//make a new array to include neighbors
const neighbors = []
//destructure the node to get th row and col([row,col])
const [row, col] = node
//write the values that we need to add and subtract from row and col to find each neighbor including diagonal neighbors
newNodes = [
//top
[-1, 0],
//bottom
[1, 0],
//left
[0, -1],
//right
[0, 1],
// [row - 1, col], //top
// [row + 1, col], // bottom
// [row, col - 1], // Left
// [row, col + 1], // right
]
//get value for the current node
const currentNode = matrix[row][col]
//for each of the new nodes array add them to current node to get the new nodes
newNodes.forEach(nodes => {
//destructure nodes new get row col node
const [newRow, newCol] = nodes;
//add new nodes value for row
const neighborRow = row + newRow;
//add new nodes value for col
const neighborCol = col + newCol;
//make sure the new nodes does not go out of bounds
if (((neighborRow >= 0 && neighborRow < matrix.length) && (neighborCol >= 0 && neighborCol < matrix[row].length))) {
//get new node's value
const neighborNode = matrix[neighborRow][neighborCol]
//see if difference between current node and neighboring nodes are (-1,0,1)
if (neighborNode < -5) {
//push the neighboring node into the new array that we made for neighbors
neighbors.push([neighborRow, neighborCol])
}
}
})
return neighbors
}
function trenchTraversal(node, matrix, visited) {
// Don't bother traversing if it is to shallow
// Traverse the potential trench to count it's length
// Your code here
const [row, col] = node
const stack = [node]
let pathArray = []
visited.add(node.toString())
while (stack.length) {
const currentNode = stack.pop()
//current
// THE THING
pathArray.push(currentNode)
// console.log(pathArray)
const [currentRow, currentCol] = currentNode
// const currentNode =
// if (visited.has(neighbor[0] - 1 && neighbor[1] - 1)) {
// return false
// }
matrix[currentRow][currentCol]
const neighbors = findNeighbors(currentNode, matrix)
// neighbors.forEach(neighbor => {
// const neighborString = ([neighbor[0], neighbor[1]].toString())
// if (!visited.has(neighborString)) {
// visited.add(neighborString)
// stack.push(neighbor)
// }
//
// })
for (let neighbor of neighbors) { //gonna go through each possible neighbor and add it stack
let neighborStr = neighbor.toString()
if (!visited.has(neighborStr)) {
visited.add(neighborStr)
stack.push(neighbor)
}
}
if (pathArray.length >= 3) {
// console.log(true)
return true
}
}
return false
}
function identifyTrench(matrix) {
// Start at 0,0 and attempt to traverse any unvisited nodes
// Your code here
for (let i = 0; i < matrix.length; i++) {
// Iterate through the columns of the matrix
for (let j = 0; j < matrix[i].length; j++) {
// Check if the current node is deeper than -5
if (matrix[i][j] > -5) {
// Check if the node has a neighbor that is deeper than -5 and is on the same row
if (j > 0 && matrix[i][j - 1] > -5) {
return true;
}
// Check if the node has a neighbor that is deeper than -5 and is on the same column
if (i > 0 && matrix[i - 1][j] > -5) {
return true;
}
}
}
}
return false;
}
TEST SPECS:
describe('findNeighbors', () => {
const sonar_data = [
[-8, -5, -9],
[-6, -5, -8],
[-7, -7, -8]
];
it('Only returns NSEW neighbors depth < -5', () => {
const neighbors = findNeighbors([1, 1], sonar_data);
expect(neighbors.length).to.equal(3);
expect(neighbors).to.deep.include.members([[2, 1], [1, 0], [1, 2]]);
});
it('Can find the correct neighbors in a corner', () => {
const NWNeighbors = findNeighbors([0, 0], sonar_data);
const SENeighbors = findNeighbors([2, 2], sonar_data);
expect(NWNeighbors.length).to.equal(1);
expect(NWNeighbors).to.deep.include.members([[1, 0]]);
expect(SENeighbors.length).to.equal(2);
expect(SENeighbors).to.deep.include.members([[2, 1], [1, 2]]);
});
});
describe('trenchTraversal', () => {
const sonar_1 = [
[-5, -5, -5, -5, -5],
[-5, -8, -8, -9, -7],
[-5, -5, -5, -5, -8],
[-5, -5, -5, -5, -5]
];
const sonar_2 = [
[-5, -5, -5, -6, -5],
[-5, -7, -8, -9, -5],
[-5, -5, -5, -8, -5],
[-5, -5, -5, -5, -5]
];
const sonar_3 = [
[-5, -5, -5, -5, -5],
[-5, -5, -5, -5, -5],
[-5, -9, -9, -5, -5],
[-5, -5, -5, -5, -5]
];
it('Can traverse a valid trench and return true, but returns false if the start is too shallow', () => {
let visited1 = new Set();
const shallowStart = trenchTraversal([0, 0], sonar_1, visited1);
let visited2 = new Set();
const validTrench = trenchTraversal([1, 1], sonar_1, visited2);
expect(shallowStart).to.equal(false);
expect(validTrench).to.equal(true);
});
it('Can traverse a valid trench and return true, but traversing a trench with a "T" returns false', () => {
let visited1 = new Set();
const validTrench = trenchTraversal([1, 3], sonar_1, visited1);
let visited2 = new Set();
const tTrench = trenchTraversal([1, 1], sonar_2, visited2)
expect(validTrench).to.equal(true);
expect(tTrench).to.equal(false)
});
it('Can traverse a valid trench and return true, but a trench that is too short returns false', () => {
let visited = new Set();
const shortTrench = trenchTraversal([2, 3], sonar_3, visited)
expect(shortTrench).to.equal(false)
});
});
describe('identifyTrench', () => {
const sonar_1 = [
[-5, -5, -5, -5, -5],
[-5, -8, -8, -9, -7],
[-5, -5, -5, -5, -8],
[-5, -5, -5, -5, -5]
];
const sonar_2 = [
[-5, -5, -5, -6, -5],
[-5, -7, -8, -9, -5],
[-5, -5, -5, -8, -5],
[-5, -5, -5, -5, -5]
];
const sonar_3 = [
[-5, -5, -5, -5, -5],
[-5, -5, -5, -5, -5],
[-5, -9, -9, -5, -5],
[-5, -5, -5, -5, -5]
];
const sonar_4 = [
[-5, -5, -5, -5, -5],
[-5, -5, -5, -5, -5],
[-5, -3, -3, -5, -5],
[-5, -5, -5, -5, -5]
];
const sonar_5 = [
[-7, -8, -5, -5, -5],
[-5, -5, -5, -5, -5],
[-5, -3, -9, -8, -5],
[-5, -5, -5, -7, -5]
];
it('Finds and identifies a valid trench, but rejects trenches that have a T', () => {
const validResult = identifyTrench(sonar_1);
const rejectT = identifyTrench(sonar_2);
expect(validResult).to.equal(true);
expect(rejectT).to.equal(false);
});
it('Rejects trenches that are too short', () => {
const rejectShort = identifyTrench(sonar_3);
expect(rejectShort).to.equal(false);
});
it('Can find a valid trench after rejecting an invalid one, and can handle data with no trenches', () => {
const noTrench = identifyTrench(sonar_4);
const secondTrench = identifyTrench(sonar_5);
expect(noTrench).to.equal(false);
expect(secondTrench).to.equal(true);
});
});
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started