Need help with the Record Collection exercise

This has proved a difficult exercise for me. I have looked at the hints and had to go all the way to the solution, sadly, to pass the exercise. That being said, I have some questions regarding my code and the solution code.

Currently this is my code:

function updateRecords(id, prop, value) {
  if(value === ''){
    delete collection[id][prop];
  }
  else if(prop === 'tracks' && collection[id][prop] != 'tracks'){
    collection[id][prop] = [];
    collection[id][prop].push(value); 
  }
  else if(collection[id][prop] === 'tracks' && prop === 'tracks'){
    collection[id][prop].push(value);
  }
  else{
    collection[id][prop] = value;
  }
  return collection;
}

This gets me to pass all of the requirements except for appending the tracks array for record number 2468
So my first question is regarding the following line of my code:

  else if(collection[id][prop] === 'tracks' && prop === 'tracks'){
    collection[id][prop].push(value);
  }

I wrote this with the intention of: if an album already had the array property tracks then the value would be pushed onto the end of the array. However, it does not push onto the array, it instead replaces the array with the value. So i should have 3 values in the tracks array property however, after execution of this line there is only 1 value. Why is this?

The solution code is listed below:

function updateRecords(id, prop, value) {
  if(value === "") delete collection[id][prop];
  else if(prop === "tracks") {
    collection[id][prop] = collection[id][prop] || [];
    collection[id][prop].push(value);
  } else {
    collection[id][prop] = value;
  }

  return collection;
}

My second question is in regards to the following line of the solution code:

collection[id][prop] = collection[id][prop] || [];

What exactly is going on here? I don’t understand why the || is following the collection[id][prop] after its assignment. Does this read "collection[id]prop] is assigned (itself) OR an array?

Any explanations and help understanding would be greatly appreciated! :slight_smile:

This

if (collection[id][prop] === 'tracks' ...

Is only true if the value of the property is the literal string “tracks”. So it will never be true. As it is, that doesn’t matter, because for “tracks”, this runs every time:

if(prop === 'tracks' && collection[id][prop] != 'tracks')

The value of collection[id]['tracks'] is never the string “tracks”, so every time prop is "tracks", you replace the current value with an array containing the new value only.

I see…

So how do i compare if collection[id][prop] is collection[id]['tracks'] in order to trigger the appending of an array?

You are already checking that prop is tracks, you need to check if it exists on the record collection object

Or

if ("tracks" in collection[id])

Or in the example, it sets collection[id].tracks to be an empty array if it doesn’t exist.

Or you could check if it’s an array (if it isn’t, you can assume it’s not there):

if (Array.isArray(collection[id].tracks)

I understand the various ways to solve this but I would like to know how to solve it without using code that has not yet been explained in the course.

Array.isArray

has not been covered nor has. How to check that prop is “tracks” without more advanced code?

"tracks" in collection[id}   

has not been covered either.

How is this solved with only the code taught up to that point? (unless it was covered andI missed it.)