Record Collection. I Need Help!

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

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

what kind of help do you need? what do you not understand?

I cant pass the challenge. I dont know what wrong with my code.

this is what I get in my code:
After updateRecords(5439, “tracks”, “Take a Chance on Me”), tracks should have “Take a Chance on Me” as the last element.

this is your code for when prop === "tracks"

if the object doesn’t already have the tracks property you create an empty array, and nothing else after that. You are not adding any value to this new array.

1 Like

I got it. Thank you ma’am for helping.

here’s my code now:

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