Learn Basic JavaScript: Record Collection

For this challenge: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection , I have this code:

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

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

What am I doing wrong here?

And would collection[id].prop work?

collection[id].prop won’t work, because prop is a variable here and dot notation can’t be used when variable is used as a key.

Keep in mind that conditions in the if/else if clause is are checked one by one from top to bottom, if one of them is true then the conditions below are ignored.

Take another look at the challenge requirements, your function in some cases takes different action than it should. For example if the collection doesn’t have key that’s passed as prop your function will always add it, or the only case when key can be deleted is if prop is “tracks” and corresponding key in the collection have empty value.

This is what I have now:

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

  if (prop === "album" || prop === "artist" && value !== "") {
    collection[id][prop] = value;
  } 
  
  if (prop === "tracks" && collection[id].tracks.length !== 0) {
    collection[id].tracks.push(value);
  } else if (prop === "tracks" && collection[id].tracks.length === 0) {
    delete collection[id].tracks;
  }
  return collection;
}

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

All but three of the tests pass. What am I still doing wrong?

what should happen if value is an empty string? I don’t really see something for that in your code
are you sure that the below is what should happen when that condition is satisfied?

try to do one paragraph at a time, and see if there is a piece of your code that clearly does that. Your code flow is a bit scattered.

I tried adding a condition that I think should work with an empty value when prop isn’t tracks. Still doesn’t work though.

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

  if (prop === "album" || prop === "artist" && value !== "") {
    collection[id][prop] = value;
  } 
  
  if (prop === "tracks" && collection[id].tracks.length !== 0) {
    collection[id].tracks.push(value);
  } else if (prop === "tracks" && collection[id].tracks.length === 0) {
    delete collection[id].tracks;
  }
  return collection;
}

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

Okay, now I’m really close. Just the test for the situation for the tracks property doesn’t exist and I have add an empty array before pushing the value to it isn’t passing. Here’s my current code:

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

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

I also tried to do it like this:

else if (prop === "tracks" && value !== "" && !collection[id].hasOwnProperty(prop)) {
    collection[id].tracks = [];
    collection[id].tracks.push(value);
  }

But that didn’t work either. What am I doing wrong? And is everything else really okay?

I solved this. I was pretty close. So this thread isn’t needed anymore.