Trouble Creating a Property on an Object

Tell us what’s happening:
While working on the code below I continue to get the message “TypeError: Cannot create property ‘tracks’ on number ‘5439’”. I am not sure what I am doing wrong while trying to create the property. Can anyone please offer advice?

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) {
 
//validate tracks is a property
if(id.hasOwnProperty('tracks') !== true){
    id['tracks'] = [''];
}
//Add value to property
else if(value != '' && id.hasOwnProperty('tracks')){
      id['tracks'].push(value);
}
//if value is empty, delete prop    
   if(id.prop.value == ''){
   delete id[prop];
  }
 return collection;
}

updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36.

Challenge: Record Collection

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

Hi ! :smiley:
your id is not your collection.

... 

// Only change code below this line
function updateRecords(id, prop, value) {
 
//validate tracks is a property
if(id.hasOwnProperty('tracks') !== true){
    id['tracks'] = [''];
}
//Add value to property
else if(value != '' && id.hasOwnProperty('tracks')){
      id['tracks'].push(value);
}
//if value is empty, delete prop    
   if(id.prop.value == ''){
   delete id[prop];
  }
 return collection;
}

....

/* here
typeof id    === number
typeof prop  === string
typeof value === string
*/
updateRecords(5439, "artist", "ABBA");

/* Tips  1*/
function updateRecords(collection, id, ...other_params)  { 
 /* Do something */
 return collection;
} 

/* Tips 2 */
// you can do this for test props in object
!collection[id].track  // if true prop track undefined
collection[id].track   // if true prop track defined

it is right, numbers do not work like objects, you can’t create a property on them
id is just a number
collection is the object

Ah ok I see my mistake.