I found this challenge to be the most difficult. I hope they break it up into small challenges as it is just not explained properly. I passed the challenge but still don’t understand how the code worked.
var collection = { // <== First level "collection"
...
...
5439: { // <== Second level "id"
album: "ABBA Gold" // <== Third level "prop"; value is "ABBA`
}
};
So the line collection[id][prop] = value; goes to a variable collection, then searches for property named id (5439) and inside that id property searches for a prop property (in this case artist). If there is one, it assigns the value of value if there isn’t, it creates new name: value pair (in this case artist: "ABBA").
Hi i found this challange difficult as was not understanding properly what needed to be done, thankfully this forum has cleared it up. the above code fails and if you look at the following line from the code i will explain why… if (!collection.hasOwnProperty(“tracks”)) { // moved from outside loop
its checking to see if the collection has the property “tracks” and then using the ! to flip the answer eg if true changing to false … if false change to true; problem its always going to be false fipped to true. As collection dosent have a property “tracks” Collection properties are the id eg 2548 2468 etc …
its the ids that have or have not the property “tracks” eg 2468 has the property “tracks” 5439 dose not have the property "tracks"
so if you change slightly the line of code i have in this post youll fix the problem
A big help is to create a file that you can debug using the developer tools online so you can step through the code. This is how i figure out whats going on and where im going wrong.So what i did was create a html file linked to a js file … open in browser and then i can debug and step through the code to see whats going on. This makes doing tricky challenges so much easier.
if you need some help post what you have got and explain where your stuck ill be around every so often and would be willing to help as im sure others would also.
is there any way we can do beside the function .hasOwnProperty? I tried to use to detect the “tracks”'s length to find out if it has “tracks” there, but it didn’t work.
I think the part in the description where we have to create an empty array: "If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property."
should be more like this:
“If prop is “tracks” and value isn’t empty but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.”
I think I understand what you’re doin here, and so I also used your code… all tests pass except for:
After updateRecords(2468, "tracks", "Free"), tracks should have "1999" as the first element.
If you see the new table, it looks like this:
collection = { "1245": { "artist": "Robert Palmer", "tracks": [] }, "2468": { "album": "1999", "artist": "Prince", "tracks": [ "Free" ] }, "2548": { "album": "Slippery When Wet", "artist": "Bon Jovi", "tracks": [ "Let It Rock", "You Give Love a Bad Name" ] }, "5439": { "album": "ABBA Gold" } }
Notice how "2468": { "album": "1999", "artist": "Prince", "tracks": [ "Free" ] } , the tracks have been erased and then “Free” was pushed. how did you get this to work for you?