Record Collection Question. WARNING spoiler

While I figured out how to pass the test on record collection I am having trouble understanding why it seems that there are extra steps that shouldn’t be required.

Please take a look at line 43 - 46 of my code that I have in comments to show how my code does not pass the second criteria. If I uncomment that block the code will pass. My question is why? Why do I have to write a condition specifically for tracks while it is the same as all other properties? Should not the code from line 50 to 53 cover the code from 43-46?

I’m a novice coder and lack formal training so I want to hear what you all think? Am I being a “lazy” coder here? Or am I missing something?

The last uncommented else if branch you wrote is both a) incorrect and b) violates the requirement.

a) Incorrect
You set the prop to an empty array with an empty string inside, then immediately after, you override the array with value. So, if value = 5 the tracks will end up being 5 instead of [5]. You are supposed to add on to the tracks not replace it.

b) Violates the requirement
The only prop that should be initialized with empty array is tracks. You are expanding this requirement to any prop. Should album be initialized with an empty array, when collection[id] doesn’t have that prop?

Also, initializing an array with empty string is asked no where on the requirement. If you do this, then every newly created tracks will start with [""] i.e) you waste a single element space and must ignore it whenever you access tracks.

1 Like

Thanks for responding.
a) i see your point but I see my code as valid because this only runs when the property does not exist and when I set the value of the array to a specific value it is only when the array is empty. If the property exist my code before these lines will pick up the slack and push instead of set the value to the array as the last item.
b) while you are right in the fact that the assignment here does not explicitly mention to create arrays for other properties the Tests does give the scenario where this will be required. For example the first test is to add artists ABBE to ID 5439. How do you pass this requirement if we only look for “tracks”

Back to a)
Suppose you are dealing with the collection[id] like this one:

{
    "album": "ABBA Gold"
}

prop = 'tracks' and value = 5

Then, you arrive at 3rd branch

else if (!collection[id].hasOwnProperty(prop)) {
    collection[id][prop] = [""]
    collection[id][prop] = value
}

The first line of the body runs and your object will look like this:

{
    "album": "ABBA Gold".
    "tracks": [""]
}

The next line runs and your object will look like this:

{
    "album": "ABBA Gold".
    "tracks": 5
}

Now, does this look right?

b) “an artist” not “artists”, in the challenge only tracks is plural. So, you shouldn’t initialize any other property with an array. Regardless, you are overriding the initialized array anyway; the initialized array is pointless.
(This is based on my point (a) )

Am I clear?

thanks again for the quick response this really helps me learn. please keep them coming.

a) I see your point thanks for writing it all out for me. so because i’m setting the key for this property it is no longer an array and it can be a problem for tracks for there are several. then in the case where it is other properties that are not tracks the test is expecting just a string and not an array so therefore the code doesn’t pass. Am I getting the cut of your jib here?

Yep, that’s about right.

You can look at the problem in a very simple way too

if no value
    delete
if prop is tracks
     if object dont have tracks, make one
     push to value to tracks
otherwise
     just set the value to whatever given prop