Help understanding record collection challenge

Tell us what’s happening:
The video provided was very helpful for this challenge, however I have a question about the solution. I want to be sure I am fully understanding this before I move on to the next challenge.
in the code:
else if (prop === “tracks”) {
collection[id][prop] = collection[id][prop] || ;
collection[id][prop].push(value);
}

I understand that this checks for a tracks prop, then creates one if there isn’t one and pushes the added value to the end of the array. My question involves the line “collection[id][prop] = collection[id][prop]”. I don’t fully understand how this makes track: appear if it wasn’t already there. I understand how “tracks” gets there, but how does the colon appear? I passed this challenge but not entirely confident of how I did so. I’m really just trying to understand how the collection[id][prop] = collection[id][prop] functions and what its full purpose is in the code.

SPOILER ALERT!!!
Your code so far


// Setup
var collection = {
2548: {
  album: "Slippery When Wet",
  artist: "Bon Jovi",
  tracks: [
    "Let It Rock",
    "You Give Love a Bad Name"
  ]
},
2468: {
  album: "1999",
  artist: "Prince",
  tracks: [
    "1999",
    "Little Red Corvette"
  ]
},
1245: {
  artist: "Robert Palmer",
  tracks: [ ]
},
5439: {
  album: "ABBA Gold"
}
};

// Only change code below this line
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;
}

updateRecords(2468, "tracks", "hs");
console.log(collection)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

Hello there,

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

The || operator returns whichever is first truthy. So, if collection[id][prop] exists, assign it to collection[id][prop]. If it does not exist, assign an empty array to collection[id][prop]

Read more about the use of || here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR

Hope this helps

1 Like