Silly Challenge 13 Error


#1

This was failing:

const letters = 'ABCDEFGHIJ'.split('');
const findAll = (g,a) => {
    GRID.forEach((e,f) => e.forEach((h,i) => { if(h===g) a.push(letters[i]+(f+1)) }));
    return a;
};
const allRocks = () => findAll('^', []);
const allCurrents = () => findAll('~', []);

Guess what the error was?
Even though GRID only goes to J, it needed more letters.

const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');

Upon adding K to Z it passed.


#2

I was bothered by this being a multi-lined solution and so I fixed it.

People argue that when code becomes too terse it becomes difficult to read, and I do agree it can be a little difficult on the eyes, but I like things to be compact when possible.

const findAll = (g) =>[].concat.apply([],(GRID.map((e,f)=>e.map((h,i)=>(h===g)?letters[i]+(f+1):false).filter(z=>z))))

Usage is then:

const allRocks = () => findAll('^') 
const allCurrents = () => findAll('~')

I’m open to further condensing it if anyone has any input on how it can be made even shorter .


Challenge 20 - Map Function Help
#3

@willcdotca Thanks for posting this. I am trying to apply the the mapping to the current challenge and I came back to reference this post.

I can see how you use “e” to pass forward an array (i.e. the row) to then be mapped again via “h” to reference the column contents of the row “e”, followed by utilizing the variables “f & i” to create your coordinate if the conditional returns true.

However, I am also now curious how that last piece of your code works:

.filter(z=>z)

Does the above “z” link up to the returned value of true after your conditional check of (h===g)? to allow the filter to proceed to “reduce” to the needed coordinates?

Thanks!


#4

Hello C_Slater, thanks for reaching out :smiley:

We’re using map to cycle through the array and replace the contents of the array with either the coordinates (B1, C2, etc) or false (for anything that doesn’t pass the conditional statement “h===g” ie. is the content “^” or “~”, etc.)

After that map cycles through we’re left with an array resembling [false,false,false,“C4”, false, false,“C7”, false false]

Filter is then going through and checking whether z is true, if so, keep it, if false, remove it.
This results in an array that contains only [“C4”, “C7”]

Map replaces contents in the array but doesn’t remove contents.
We could use filter instead of map, but the we’d be left with ["~","~"] instead of [“C4”, “C7”] and lose the index data to know which position they were in (f & i)

Does that help clarify?

In short .filter(z=>z) is cycling through and removing everything in the array that is false.


#5

Your explanation is the best piece I have read to date on this map/filter idea. This makes perfect sense! Thank you :partying_face: