Having some trouble with 13


#1

Im trying to figure out how to solve 13 but its saying my code ran but didnt produce the correct result. Im going to vancouver today and wont be able to access this site so Im really trying to solve it now and I tried at 3 am too!
function allRocks() {
let filteredList = GRID.filter(rock => {
return rock[0] === “^”;

});

filteredList;}

function allCurrents() {
let filteredList = GRID.filter(current => {
return current[0] === “~”;
});

filteredList;}

this is my code. can someone help?


#2

I’m at a similar stage. Not really grasping the arrow functions yet but here is what I have noted so far… sill trying to piece it together. Also, please note that I am new to Javascript, may be overthinking this? What follows is my “train of thought” as I struggle to find a solution!

  1. we already have a function isRock() which should return true if the given cell contains a rock. I am assuming it is preferable to use this over the string indexing comparison. Not really sure how to incorporate this yet.

  2. the filter method (https://www.digitalocean.com/community/tutorials/how-to-use-array-methods-in-javascript-iteration-methods#filter() as seen in the hint) returns what seems to be the VALUE not the index… however the challenge wants us to return the coordinates. I am missing the link here on how to convert the value back to an index to then allow for conversion back to coordinates (i.e from ^ to GRID[3][2] to D3).

  3. Building on the index, I have looked into indexOf() which seems to only return the first value when matched but on the following page their is an example on how to return all occurrences within an array through the implementation of a loop. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

  4. To make this even more difficult, we are looking at an array of objects (two indexes GRID[0][0] ) I am still also trying to determine how this gets incorporated into the above notes.

I am going to try and write some pseudo code to help bring this together. To anyone reading this that finds a solution or sees me falling into a rabbit hole with the above, PLEASE LET ME KNOW! lol


#3

The arrow notation is the new way to write a function. One difference that I know of is that arrow functions automatically bind the context of this to your function.

  1. I was also looking at using isRock() but that takes in a coordinate of the GRID, not indexes so it might be too much work to convert every index pairs to grid coordinates and then do the check with isRock() but no one is stopping you from that.
  2. I didn’t use filter because as you said, it returns the value but what we need is index.
  3. I think this could work although I didn’t use indexOf() when I implemented my solution for this challenge.

Now what I did in my solution is that I had a for-loop within a for-loop where the outer loop will be my row index and the inner loop will be my column index. Then within the nested loop, I would do a check if there is a rock on those index pairs. If so, then I used my helper function that would convert those index pairs to a grid coordinate string. That would then be pushed to my result array. In the end, I return that result array.

Also I refactored that functionality so that I can use it for both allRocks() and allCurrents().

Hope that helps.


#4

Thanks for the suggestions! I am also at the point of using two loops to iterate through the array as you mentioned. However, I was really hoping to use the hinted filter function to extend my learning as a few challenges back I utilized a “simpler” (maybe familiar is a better word) to achieve the needed result.

Also, thanks for the note on isRock(), got caught up in one train of thought and hadn’t considered that yet :slight_smile:


#5

If anyone gets filter() working as the hint suggests please consider reaching out to me via DM. I would enjoy seeing an alternative solution to the nested loops as discussed above.

New to JS and I just couldn’t wrap my head around how to use filter() and map() together to not only return the values matched but also modify the returned array concurrently. I could only get it to return an array of ["^", … “^”], only the values that matched the condition checked.

Daily challenge is complete but wow did the difficulty ever ramp up today! :sweat:


#6

I’m in the same boat as you. Could use a loop, but I’m really interested to see how filter could work. If you get a response, could you DM it to me as well?


#7

Here is what I think I have to do/have figured out so far:

  1. Scan each row and return only the rock strings in a single array. I set up a loop that did this and used the filter method. What it did however, was create an array of arrays where only the ‘^’ were included but rows it now rock strings were also included as empty arrays:

function allRocks() {
let col = [];
for (let i = 0; i < GRID.length; i++) {
col.push(GRID[i].filter(item => (item === ‘^’)));
}
return col;
}

Calling allRocks(); produced:
[ [ ‘^’ ],
[],
[ ‘^’, ‘^’ ],
[ ‘^’, ‘^’ ],
[],
[],
[],
[ ‘^’, ‘^’ ],
[ ‘^’ ],
[ ‘^’ ]
]

I don’t know if that is helpful but at least I’m able to somewhat filter the rock strings.

  1. I also know I have to take each rock string and turn it into the appropriate Grid coordinate. Ex. The rock at D1 has to go from ‘^’ to GRID[0][2] to ‘D1’

I came up with a function that alphabetizes the column index:
function alphabetize(n) {
return String.fromCharCode(97+n).toUpperCase();
}

But I’m not sure where I can fit that in. I feel like another loop may be necessary


#8

@Scramblelock Looks like you’re not understanding how the filter() method can be applied to this situation.

Check out this thread: Challenge 13 Filter :wink:


#9

After filtering the results you will need to concatenate the result making a 1 dimensional array out of the multidimensional filtered array that is returned

From:
[[],["B2","B5"],[],["D5"],[]...]

To:
["B2","B5","D5"...]

(I just typed random examples, not to be take literally, but used as reference for appearance sake)