Challenge 20, code produces correct result, not accepted


#1

Hi,

I solved 20 procedurally as I have little experience using functional programming in JS but my results are not accepted

function safetyReport(){
let result = [];
for ( var i = 0; i < GRID.length; i++ ) {
result[i] = [];
}
for(var row = 0; row < GRID.length; row++){
for(var col = 0; col < GRID[row].length; col++){
let cell = String.fromCharCode(col + 65) + (row + 1);
result[row].push(howDangerous(cell));
}
}
return result;
}
which produces

[[0, 0, 0, 100, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 50, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 100, 100, 0, 0, 0, 0],
[0, 0, 0, 0, 100, 100, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 100, 50, 50, 0, 0, 0, 100, 0, 0],
[0, 100, 0, 50, 50, 0, 0, 0, 0, 0],
[0, 100, 0, 0, 50, 50, 0, 0, 0, 0]]

which seems to be right when tested against other peoples results but still I get
“Your code ran but did not produce the correct result.”


#2

I had the exact same error, but (with advice from a friend), replaced the call to howDangerous() with an if/else if/else statement, everything passed.

i.e.

if (GRID[row][col] === "^") {
  result[row].push("100");
} else if (GRID[row][col] === "~") {
  result[row].push("50");
} else if (GRID[row][col] === "") {
  result[row].push("0");
}

So, really, all you need to make this function work is GRID and no other function(s).
I’m still just as puzzled as you are, especially as the hint says to reuse old code…


#3

Hey man you didn’t get the outcome because you only needed to replace the rocks, currents, and ships. Everything else should still be an empty string. So it will look like the grid below.

["", “”, “”, 100, “”, “”, “”, “”, “”, “”]
["", “”, “0”, “”, 50, “”, “”, “”, “”, “”]
["", “0”, “”, “”, 100, 100, “”, “”, “”, “”]
["", “”, “”, “”, 100, 100, “”, “”, “”, “”]
["", “”, “”, “”, “”, “”, “”, “”, “0”, “”]
["", “”, “”, “”, “”, “”, “”, “”, “”, “”]
["", “”, “”, “”, “”, “”, “”, “”, “”, “”]
["", 100, 50, 50, “”, “”, “”, 100, “”, “”]
["", 100, “”, 50, 50, “”, “”, “”, “”, “”]
["", 100, “”, “”, 50, 50, “”, “”, “”, “”]


#4

I have a similar problem here, I’ll try the if else statement but doesn’t have any sense when they are asking you to reuse code.

function safetyReport() {
  return GRID.map((row, rowIndex) => {
    return row.map((cell, cellIndex) =>{
      return howDangerous(String.fromCharCode(65 + cellIndex) + (rowIndex + 1));
    });
  });
}

In codepen looks like is working https://codepen.io/lucasarvelo/pen/aRNmLZ