Challenge 13 - Test cases are correct but not passing {SOLVED}


#1

My code works when I run it in JSFiddle, but the site keeps telling me that my output is incorrect.

Code:

const coordMap=[].concat(...GRID).map((_,i)=>`${String.fromCharCode(i%10+65)}${Math.floor(i/10)+1}`)
const allRocks=()=>coordMap.filter(isRock);
const allCurrents=()=>coordMap.filter(isCurrent);

The sample test case passes:
image

First line is my code’s allRocks() output, second line is the sample allRocks() output
Third line is my code’s allCurrents() output, second line is the sample allCurrents() output

Perhaps it’s a problem with how the interpreter checks if it’s equal? After all, == doesn’t work for checking the content in arrays.


#2

I don’t have an answer for you, just wanted to say ‘awesome code!’. Spent the last couple of minutes breaking it down. I like how you flattened out the GRID first, returning all of the coordinates, then filtered it with the isRock callback.

It works in my testing environment as well, I’m not sure why it doesn’t work in the lighthouse environment.


#3

I went with simpler functionalities, but still produce the same results and my code doesn’t past the test… It’s weird! Here it is:

var rocks = [];
var currents = [];

function allRocks() {
  for (y = 0; y < GRID[0].length; y++) {
    for (x = 0; x < GRID.length; x++) {
      if (GRID[y][x] === '^') {
        cx = x + 65;
        px = String.fromCharCode(cx);
        cy = y + 1;
        rocks.push(px + cy);
      }
    }
  }
  return rocks;
}

function allCurrents() {
  for (y = 0; y < GRID[0].length; y++) {
    for (x = 0; x < GRID.length; x++) {
      if (GRID[y][x] === '~') {
        cx = x + 65;
        px = String.fromCharCode(cx);
        cy = y + 1;
        currents.push(px + cy);
      }
    }
  }
  return currents;
}

When i call my functions in the console, I get these two answers:

["D1", "E3", "F3", "E4", "F4", "B8", "H8", "B9", "B10"]
["E2", "C8", "D8", "D9", "E9", "E10", "F10"]

Any idea why the challenge doesn’t accept it?


#4

Your code is working in my test environment as well.

I read a lot that some people logout then log back in and re-submit, then it works for them. I personally have not had to do this, but may be worth a shot. Save a copy of your code first, so you don’t loose it.

Actually, its not working in my second test environment. It seems that it doesn’t like that you are declaring the arrays outside of the functions. Making them global. Therefore if their test environment calls the function allRocks more than once, it keeps adding to the global array rocks, causing the array to get larger and larger. That is what is happening in my one test environment. Try declaring rocks and currents inside the respective functions.


#5

ayanokouji, Check if your code works with more than 10 columns.


#6

Dom, Y in your code is column, but you are checking the value with GRID[Y][X], try to change to GRID[X][Y].


#7

ddonini, I’ve refactored it accordingly and it works now. Thanks!


#8

Check GRID[0].length (what it mean?) your for loop need to be changed.


#9

Hey @paulbeaudon, c’est pas fou ça! I didn’t think of the scenario of running it twice. Thanks for the tip!


#10

Hi @ddonini, it creates the expected result in the test environnement. I think paulbeaudon’s solution will work. Thanks!


#11

Hi @Lana, it already creates the expected result in the test env. GRID[0].length is the lenght of the sub-array at the 0 position, which returns 10, the width of the array.


#12

You may see the result in the test environment that is expect result. But if you think about logic GRID[0].length mean row first in GRID table. It mean you already know there are 10 row, 10 column, why you don’t just put it 10. It also mean you’re not loop to the same row.


#13

Not sure why this isn’t working. I’m reading through the comments to try to see if someone has a similar problem. My test cases pass (even when I modify the grid).

Here’s my code, you can run allCurrents() and allRocks() in the console to test
https://jsbin.com/yitojejegu/edit?js,console

EDIT - Ugh… the problem wasn’t the code. I just accidentally pasted the GRID code in, and that screwed everything up.


#14

nevermind, ignore me


#15

@lana, we know the grid being used by this lighthouse is 10x10, but the code we write has to be used by other lighthouses as well so we need to accommodate grids of variable size up to 26x26.


#16

You may not understand what I mean. Using GRID[0].length is not good way.