Basic JavaScript: Record Collection stuck

Tell us what’s happening:
Can someone help me with this? I’ve been trying to solve this with the help of other forum topics and videos, but I keep getting “TypeError: Cannot read property ‘5439’ of undefined” and nothing is checked at all.

Your code so far


// Setup
var collection = {
2548: {
  albumTitle: 'Slippery When Wet',
  artist: 'Bon Jovi',
  tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
  albumTitle: '1999',
  artist: 'Prince',
  tracks: ['1999', 'Little Red Corvette']
},
1245: {
  artist: 'Robert Palmer',
  tracks: []
},
5439: {
  albumTitle: 'ABBA Gold'
}
};

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

updateRecords(collection, 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/86.0.4240.111 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

Hi and welcome to the forum!

You should never reference the global variable collection.

okay, thank you, got rid of it, still, get the same message in the console though

It also says
'TypeError: Cannot read property ‘5439’ of undefined
at updateRecords (/script.js:33:22)
at /script.js:39:1" in Repl.it console

What is your current code?

function updateRecords(object, id, prop, value) {

  if (value === "") {

    delete collection[id][prop];

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

    collection[id][prop] = 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);

  }

  return object;

}

updateRecords(collection, 5439, 'artist', 'ABBA');

You are still referencing the collection object but have now deleted the starter code with that object. collection is undefined, which is what generates the error.

You need to leave the collection object in the starter code but never reference the collection object in your function. You need to instead reference the function argument object.

Finnaly, I’ve done it haha, thank you sooo much for the help :smile:

1 Like