Record Collection HELP?

Tell us what’s happening:

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"
    }
};
// 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].push(value);
} else if (prop === "tracks" && !collection[id].hasOwnProperty("tracks")) {
  collection[id][prop] = [];
  collection[id][prop].push(value);
} else if (prop === "tracks" && value !== "") {
  collection[id][prop].push(value);
} else if (value === "") {
  delete collection[id][prop];
} else {
  
  return collection;
}
}

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection

Throws this error:

Cannot read property ‘push’ of undefined

The test is trying to access a non-existent property.

collections[5439] only has a property for 'album', so you need to test first to see if the property you want is there, and if it’s not, create one.

Also, I don’t believe you can use push to alter any of the properties besides tracks, because it is an array. The rest are string primitives.

Hope this helps! I had a really tough time with this one too.

@anonbubble
my code is similar to yours, but without errors.

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].push(value);

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

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

I think the problem with your code is this

!collection[id].hasOwnProperty("tracks")

replace “tracks” with just prop

that should work

2 Likes

Thanks! I changed it but it is still throwing three errors, will try to figure em out anyway

1 Like

Here was the problem line:

(prop == “tracks” && !collection[id].hasOwnProperty(prop) == false)

I used what you suggested and even experimented with some things from your code. I added the “prop” too but nothing seemed to work but it was the (!) before collection that kept flipping my answer. After I removed it, the challenge was solved. Thank you so much!

1 Like

You are welcome, glad you fixed it…

2 Likes

Thanks @gudchyld . I had the similar problem with my code too.

The issue is that I was incorrectly creating an empty array as per instruction.
empty-array

After changing var prop = ; into collection[id][prop] = ;. Challenge passed. On to the next one