Challenge 13 help


#1

Hi fellow coders,
I have been stuck on challenge 13 for 5 days and can’t seem to figure out what i am doing wrong. Would appreciate any/all input here. I keep getting the error “Your code ran but did not produce the correct result” and I can’t find any issues.

Thank you!

function allRocks() {
var alpha = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
rocks = [];
for (i=0; i < GRID.length; i++){
for (j=0; j < GRID[0].length; j++) {
var coord = alpha[j] + (i+1);
if (isRock(coord)) {
rocks.push(coord);
}
}
}
return rocks;
}

Other functions:
function lightCell(cell) {
var valid;
var col = convertColumn(cell);
var row = cell.substr(1,1)-1;
if ( row < (countRows()) && col < (countColumns())) {
valid= GRID[row][col];
}
else {
valid = false;
}
return valid;
}


#2

Have a look at your lightCell() function.

The line

var row = cell.substr(1, 1) - 1;

What happens when you have a cell of A10?
console.log() to find out.

There is also the .slice() string method, which can be implemented into the function.


#3

OMG!! That was it!. It worked in the previous challenge so I didn’t even think about it. I kept focusing on the new addition to lightCell and all Rocks() function that I missed something. Thank you so much! I spend 3 days trying to figure out what I missed.


#4

I used var row = cell.slice(1)-1; and it worked :grinning: Thank you so much :):grinning:


#5

Cool beans!

Glad I could help.


#6

Another note,

...
if ( row < (countRows()) && col < (countColumns())) {
  valid= GRID[row][col];
} else {
  valid = false;
}
...

…can be simplified to a ternary,

const valid = row < countRows() && col < countColumns() ? true : false;

…but because the expression returns a boolean, the expression can be further simplified to,

const valid = row < countRows() && col < countColumns();

Just an FYI. :smile:


#7

Question: Would you return the value from GRID where

valid == true

in this case?


#8

You’re function is pushing the valid coordinate into the rocks array within the nested for-loop (i.e. by the end of the for-loop, your rocks array should be filled with all the coordinates that satisfy the requirements set within your nested for-loop), so, I would return the rocks array at the end of the function.

Let me know if you still have questions.