Record Collection Issue, type error

Record Collection Issue, type error
0

#1

I’m currently working on [Record Collection](https://www.freecodecamp.com/challenges/record-collection#?solution= %2F%2F%20Setup var%20collection%20%3D%20{ %20%20%20%20"2548"%3A%20{ %20%20%20%20%20%20"album"%3A%20"Slippery%20When%20Wet"%2C %20%20%20%20%20%20"artist"%3A%20"Bon%20Jovi"%2C %20%20%20%20%20%20"tracks"%3A%20[%20 %20%20%20%20%20%20%20%20"Let%20It%20Rock"%2C%20 %20%20%20%20%20%20%20%20"You%20Give%20Love%20a%20Bad%20Name"%20 %20%20%20%20%20%20] %20%20%20%20}%2C %20%20%20%20"2468"%3A%20{ %20%20%20%20%20%20"album"%3A%20"1999"%2C %20%20%20%20%20%20"artist"%3A%20"Prince"%2C %20%20%20%20%20%20"tracks"%3A%20[%20 %20%20%20%20%20%20%20%20"1999"%2C%20 %20%20%20%20%20%20%20%20"Little%20Red%20Corvette"%20 %20%20%20%20%20%20] %20%20%20%20}%2C %20%20%20%20"1245"%3A%20{ %20%20%20%20%20%20"artist"%3A%20"Robert%20Palmer"%2C %20%20%20%20%20%20"tracks"%3A%20[%20] %20%20%20%20}%2C %20%20%20%20"5439"%3A%20{ %20%20%20%20%20%20"album"%3A%20"ABBA%20Gold" %20%20%20%20} }%3B %2F%2F%20Keep%20a%20copy%20of%20the%20collection%20for%20tests var%20collectionCopy%20%3D%20JSON.parse(JSON.stringify(collection))%3B%0A%0A%2F%2F%20Only%20change%20code%20below%20this%20line%0Afunction%20updateRecords(id%2C%20prop%2C%20value)%20%7B%0A%20%20if%20(value%20%3D%3D%3D%20%22%22)%7B%0A%20%20%20%20delete%20collection%5Bid%5D%5Bprop%5D%3B%0A%20%20%7D%20else%20if(prop%20%3D%3D%3D%20%27tracks%27%20%26%26%20collection%5Bid%5D.hasOwnProperty(%27tracks%27)%20!%3D%3D%20true)%7B%0A%20%20%20%20%20%20collection%5Bid%5D.tracks%20%3D%20value%3B%0A%20%20%7D%20else%20if%20(prop%20%3D%3D%3D%20%27tracks%27)%7B%0A%20%20%20%20%20%20collection%5Bid%5D.tracks.push(value)%3B%0A%20%20%7D%20else%20if%20(prop%20!%3D%3D%20%27tracks%27%20%26%26%20value%20!%3D%3D%20%22%22)%7B%0A%20%20%20%20%20%20collection%5Bid%5D%5Bprop%5D%20%3D%20value%3B%0A%20%20%7D%20%0A%20%20%0A%20%20%0A%20%20%20%20%0A%20%20%0A%20%20return%20collection%3B%0A%7D%0A%0A%2F%2F%20Alter%20values%20below%20to%20test%20your%20code%0AupdateRecords(2468%2C%20%22tracks%22%2C%22raider%22)%3B%0AupdateRecords(2468%2C%20%22tracks%22%2C%22raider%22)%3B%0AupdateRecords(2468%2C%20%22tracks%22%2C%22raider%22)%3B%0AupdateRecords(2468%2C%20%22tracks%22%2C%22raider%22)%3B%0AupdateRecords(2468%2C%20%22tracks%22%2C%22raider%22)%3B%0A%0AupdateRecords(1245%2C%20%22tracks%22%2C%22raider%22)%3B%0AupdateRecords(1245%2C%20%22tracks%22%2C%22raider%22)%3B%0AupdateRecords(1245%2C%20%22tracks%22%2C%22raider%22)%3B%0AupdateRecords(1245%2C%20%22tracks%22%2C%22raider%22)%3B%0AupdateRecords(1245%2C%20%22tracks%22%2C%22raider%22)%3B%0AupdateRecords(5439%2C%20%22tracks%22%2C%20%22Take%20a%20Chance%20on%20Me%22)%3B%0AupdateRecords(5439%2C%20%22tracks%22%2C%20%22Take%20a%20Chance%20on%20Me%22)%3B%0A) and I believe that the ‘compiler’ is acting strangely (I’m probably missing something).

When I try to update ID: 5439’s tracks property twice in a row, it throws a type error `collection[id].tracks.push is not a function.

Of note, ID:5439 was the only one without a ‘tracks’ property at the start. It works fine if the function is only called once.

There are no problems when I use the function with other id’s, and it passes all tests otherwise.

Any help would be appreciated, I’ve looked and looked but can’t find a solution.


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

// Alter values below to test your code
updateRecords(2468, "tracks","raider");
updateRecords(2468, "tracks","raider");
updateRecords(2468, "tracks","raider");
updateRecords(2468, "tracks","raider");
updateRecords(2468, "tracks","raider");

updateRecords(1245, "tracks","raider");
updateRecords(1245, "tracks","raider");
updateRecords(1245, "tracks","raider");
updateRecords(1245, "tracks","raider");
updateRecords(1245, "tracks","raider");
updateRecords(5439, "tracks", "Take a Chance on Me");
updateRecords(5439, "tracks", "Take a Chance on Me");


#2

Tracks should be an array. Your first else if statement is adding tracks property and assigning a value to it and then second time you are trying to push to that value.


#3

Thanks so much, I struggled with this for a long time. I just had to create the tracks property first and set it to an empty array, and then push the value into it.


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

I was trying to create the property dynamically (is this how to say it?) by straight up selecting a property that didn’t exist and setting it equal to a value. Is there a way to create arrays doing this? And is it an accepted way of modifying objects?


#4

You could write it like this:

collection[id].tracks = [value];

#5

Ah thanks jenovs, I appreciate your help.