Stuck on challenge 14 - please help move allShips


#1

function countRows(){
return GRID.length
}
function countColumns(){
return GRID[0].length
}
function isShip(e){
return e===“v”
}

function allShips(){
let rows = countRows();
let columns = countColumns();
let box = “”;
const letters = “ABCDEFGHIJKLMNOP”;
let isShips = [];

for (let i = 0; i < rows; i++) {
for (let j = 1; j < columns; j++) {
box = GRID[i][j];
if (isShip(box)) {
isShips.push(letters[i]+j);
}
}
}
return isShips;
}

console.log(‘ships’, allShips())

what am i doing wrong now?


#2

Potential Issue:

const letters = “ABCDEFGHIJKLMNOP”;

You should consider extending this to Z for larger grid test cases.

Most likely culprit (this got me the first time through as well!):

for (let i = 0; i < rows; i++) {
for (let j = 1; j < columns; j++) {
box = GRID[i][j];
if (isShip(box)) {
isShips.push(letters[i]+j);

You have designated i for rows but you pass it into the letters array (which represents our columns) on the fifth line above. Further GRID rows are 1 to 10 (potentially higher). Your current code could potentially return 0 for a row which isnt one of our available rows on the printed grid. You could consider the following:

isShips.push(letters[j]+(i+1));

Edit: Sorry but I missed that you had j =1 in your for loop as well, you may want to set this to j=0 to ensure that you could return “A” from your letters string.