Record Collection Question --- :(

// 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": [ 
        "Little Red Corvette" 
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    "5439": {
      "album": "ABBA Gold"
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  if (prop !== "tracks" && value !== ""){
collection[id][prop] = value;
  else if (prop === "tracks" && collection[id].hasOwnProperty(prop) === false){
    collection[id][prop] = [];
    collection[id][prop] = value;

  else if (prop === "tracks" && value !== ""){


  else if (value === ""){
    delete collection[id][prop];
  return collection;

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

OK I’m scratching my head. I obviously cannot find where my code went wrong. Can someone help?

After updateRecords(5439, "artist", "ABBA") , artist should be "ABBA" – PASSED
After updateRecords(5439, "tracks", "Take a Chance on Me") , tracks should have "Take a Chance on Me" as the last element. – ERROR
After updateRecords(2548, "artist", "") , artist should not be set – PASSED
After updateRecords(1245, "tracks", "Addicted to Love") , tracks should have "Addicted to Love" as the last element. – PASSED
After updateRecords(2468, "tracks", "Free") , tracks should have "1999" as the first element. – PASSED
After updateRecords(2548, "tracks", "") , tracks should not be set – PASSED
After updateRecords(1245, "album", "Riptide") , album should be "Riptide" – PASSED

OK I just figured out what would make it work, to change:
collection[id][prop] = value

What I don’t understand is why that’s necessary to use push() since I just created an empty array, and I am setting the array to the value. There is only one value, so it shouldn’t matter whether it’s pushed onto the property or replacing it, no?

collection[id][prop] = value
This line will change collection[id][prop] to whatever value is, so it won’t be an array unless value was already an array.

Ah so I have to use the following:

collection[id][prop] = [value];


@sidneyyin Would you mind explaining why = [value]; works? Is it just stating that prop in this case is an array, so we want to add a value to that array?

im guesing it works cause javascript is very dynamic and you dont need to asign memory space so allows for a creating of a new array and putting something at the first index in one call. way to test would be to add an extra parameter and try [value, para] and im guessing it would fill out both 0 and 1 in the index.

1 Like

I am not an expert, but because = value would simply add a value to the property “tracks” of that object, whereas the challenge is asking us to create an array of tracks under that album, if it doesn’t already exist. Subtle difference, but that’s what I got errors for before I knew the difference.

1 Like

yes but by putting the value inside [] you are in javascript creating a new array and placing the value in it at the same time. most langs wouldnt allow for it but javascript is like i said very dynamic.

note i havnt tested it myself when i did the challenge i asigned new array on one line cause i come from a c background but if its working for the OP it would be logical thats how its working

1 Like