Pls help me with Basic JavaScript: Record Collection [solved]

Is it bag or I`m doing something wrong?

I have this error but I don`t understand it

// running tests
updateRecords(...)[5439].tracks.pop is not a function
collection[id][prop].push is not a function
Cannot read property 'pop' of undefined
After updateRecords(2548, "tracks", ""), tracks should not be set
// tests completed

My code:

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

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

Link to the chalange

So here’s the thing: record 5439, if you look in the given collection code, doesn’t have a tracks property. Without having an array with the property name tracks, you don’t have any of the array methods available to you.

I just wrote a long (and potentially agonizing) post on this very topic in another thread. Take a look at Help! not creating a "tracks" property and setting value to empty array [] to see what I mean. It may change the way you’re writing this, just a little.

Note that it’s not giving you the answer. It is helping you to think about the problem in a little more abstract way.

1 Like

Yes record 5439 does not have a “tracks” property so I`ve written a code which has to add this property

if (collection[id].hasOwnProperty(prop) == false){
    collection[id][prop] = value;  

Right. but if you are adding a track, you don’t simply assign the value, you have to create the array and then append the value to it.

1 Like

Oh thank you very much I finally understood

Did you solved this ? Can you please write teh code here

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.