Challenge 20 - Correct Output, did not produce correct result?


#1

My code is producing the correct output but is not being accepted. What am I missing?

12%20AM

function safetyReport(){
  for (let i = 0; i < GRID.length; i++){
    for (let ii = 0; ii < GRID.length; ii++){
      const coord = String.fromCharCode(65 + ii) + (i + 1);
      GRID[i][ii] = howDangerous(coord);
    }
  }
  return GRID;
}

#2

maybe instead of modifying the original GRID, push the results to a different array and return that instead.


#3

I ended up using the map function instead of the second for loop, and it worked, so maybe that was the difference


#4

kpalos already identified the problem, but it’s normally considered poor programming to have a function modifying a global variable inside the function.

You have no idea what other functions might be running on GRID, but now you’ve changed all the values in GRID, so those functions will now break.


#5

nested map statements also works since they dont mutate


#6

The challenge asks:

return your GRID with all of the values replaced by the percentage for how dangerous they are.

So the wording that they used is weird if that is the case, they could have said “return a grid” or “return a new grid”.


#7

Both of your for loops iterate to GRID.length, which is the number of rows. Your code wouldn’t work when the number of rows and columns are different.


#8

What should the GRID look like, with regard to empty and ship cells? I originally had howDangerous return 100 for ships and 0 for empty cells, but noticed I get an error in the LL editor for Exercise 20, saying “too many errors.” If I instead have them not return at all (i.e. Undefined), that goes away, but the “correct result is not produced.”

People who have completed this challenge, what are you returning for empty and “ship” cells?


#9

I returned the following with empty and ship cells as 0

[[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]]


#10

Hmm interesting. Ok thanks, at least that lets me eliminate that as the issue with my code :slight_smile:
Did you happen to notice if you got the “too many errors” warning in your LL editor? (It still passes, but the last line has the “!” warning)


#11

I have a warning in my code from the percentReport() which I have yet to resolve, but nothing at end of document or with my current code which passed earlier.


#12

Thanks for the hint jdpaterson!