This is a freeCodeCamp exercise about nested objects and arrays. Here are some things that helped.
Translate the Instructions Correctly and Comprehensibly
I watched the FCC YouTube tutorial. Of course I was able to pass the challenge by copying what the instructor did (see image). But this didn’t help with my goal of learning and understanding. I read some feedback from forums. Then I reworked some of the prior exercises to review the previous lessons.
Then, I returned to the challenge, went through the instructions slowly, translated them to English, and then translated that to JS code.
As simple as it might seem, this sentence tripped me up because it sounds as though id, prop, and value are the names of the elements in the collection. It helped me immensely to think of them as the parameters in the function we create. Like, really stop and think about that. It would actually help with comprehension to name these peanuts, banana, and podcast. They’re just variable names, like box labels.
The next paragraph states: “If
value isn’t empty (
""), update or set the
value for that record’s album property. This translates straightforwardly into code:
The next paragraph states, “You must always return the entire collection object.” Easy enough. At the end of your function, just before the closing curly brace, put “return collection;” This is already in the FCC workspace, so you don’t need to type it.
If you’re watching the YouTube tutorial, don’t worry about the JSON parse stringify thing. That’s just a way for the tutor to display output. You don’t need it.
This is telling you to look at the second parameter that you put into the function. In the FCC test, the second parameter (
collection using different test inputs. For this test, I made sure I used “tracks” as the test
prop. Of course, in your text editor, you also have to have the collection data structure (all the album data) set up. Here is what my function looked like at this point:
I used hasOwnProperty because that was one of the lessons we learned, and the purpose of this exercise is to reinforce those lessons.
Each time I added a rule, I tested it.
The next paragraph that we have to translate into code is: And then the next instruction is going to say “if
value is empty (
""), delete the given
prop property from the album.” That means that if you want to delete the tracks from your album’s collection, run your updateRecords function with
"" as the third parameter. This seems like an odd way to delete data, and not something you would do in the real world. So, don’t think about it in real world terms; just remember that
value is the value you put into the test function when you run it.
So, I translated these next two statements like this:
Putting it all together, with the test inputs, my function looked like the image below … and it worked!