Basic JavaScript - Record Collection

Tell us what’s happening:
Hi, I cannot figure out where my code is going wrong. I also don’t know why I am gettings this
TypeError: Cannot read properties of undefined (reading 'hasOwnProperty')

  **Your code so far**
// Only change code below this line
function updateRecords(records, id, prop, value) {
  if ([records][id].hasOwnProperty(prop)) {
    if (value=="") {delete [records][id][prop]}
    else {[records][id][prop].push(value)}
  }
  else if (value!="" && prop!="") {[records][id][prop] = value};
  return records
};

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

Thanks in advanced!

  **Your browser information:**

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

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

You cannot use dot notation with variables holding property names.

What @JeremyLT said. And if you need a refresher, here’s the lesson where this is taught: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables

1 Like

Still got one mistake. Anyone able to help me with that?

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

Tracks vs other properties need to be handled differently.

1 Like

Brcause it is an array, for anyone wondering this fixed my second problem:

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

Thanks for the help.

1 Like