Basic JavaScript - Record Collection

Tell us what’s happening:
Describe your issue in detail here.
I’ve been stuck on this question for a while and don’t know what’s wrong with my approach. I’m thinking it might be a syntax error but I’m not sure. My thought process is to check what the id is → check if value is empty → if it’s not empty than set value as the property OR add the array then add the value to the array. If someone know’s what’s wrong with my code please let me know :sweat_smile:

  **Your code so far**
// Setup
const recordCollection = {
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(records, id, prop, value) {
if (id == "artist" && value != ""){
  id.prop = value;
}
else if (id == "artist" && value == ""){
  delete id.prop;
}
else if (id == "tracks" && value != ""){
  if (id.hasOwnProperty(prop)){
    prop.push(value);
  } else if ((id.hasOwnProperty(prop)) == false){
    id.prop = [];
    prop.push(value);
  }
}
else if (id == "tracks" && value == ""){
  if (id.hasOwnProperty(prop)){
    delete id.prop;
  } else if ((id.hasOwnProperty(prop)) == false){
    return records;
  }
}
else if (id == "albumTitle" && value != ""){
  id.prop = value;
}
else if (id == "albumTitle" && value == ""){
  if (id.hasOwnProperty(prop)){
    delete id.prop;
  } else if ((id.hasOwnProperty(prop)) == false){
    return records;
  }
}

return records;
}

updateRecords(recordCollection, 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/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

id is not the same thing as prop. You are mixing the two up here.

Also, the only special case for prop should be “tracks”.

Hey Jeremy,
thank you for the tips, I’m trying out a different approach but still can’t seem to get the right solution.

function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && value !== ""){
    id.prop = value;
  } else if (prop !== "tracks" && value === ""){
    delete id.prop;
  } else if (prop === "tracks" && value !== ""){
    if (id.hasOwnProperty("tracks")){
        prop.push(value);
      } else {
        id.prop = [];
        prop.push(value);
    }
  } else if (prop === "tracks" && value === ""){
    if (id.hasOwnProperty("tracks")){
        delete id.prop;
      } else {
        return records;
      }
  return records;
}
}

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

I still think you don’t fully understand what id is.

function updateRecords(records, id, prop, value) {
  console.log(records);
  console.log(id);
  console.log(prop);
  console.log(value);
}

id.prop would mean that you want the literal property called prop from the object id. Is id an object though?

Oh is id not an object? I thought that id was a nested object inside the object recordCollection. Isn’t the syntax for an object objectname: { properties of the object} ?

What does the code I gave you show? What is id?

It shows ID as a number. So it’s not an object? I’m just confused on the {} after the id because doesn’t that mean it is an object with properties?

Look back here

Ah got it now, it’s a sub-property of the object.

function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && value !== ""){
    records.id.prop = value;
  } else if (prop !== "tracks" && value === ""){
    delete records.id.prop;
  } else if (prop === "tracks" && value !== "" && records.id.hasOwnProperty(tracks) === true){
    records.id.tracks.push(value);
  } else if (prop === "tracks" && value !== "" && records.id.hasOwnProperty(tracks) === false){
    records.id.tracks = [];
    records.id.tracks.push(value);
  } else if (prop === "tracks" && value === ""){
    delete records.id.prop;
  }
}

I tried again using records to access id and prop but I still can’t get it to work :sweat_smile:

Dot notation only works with the literal object property name. If you have a variable holding the property name, bracket notation is mandatory.

Finally got it. Thank you so much for the help.

1 Like

Good work! You did all the hard bits, I just helped you see what type the variables were.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.