Record Collection All tests passed but one and not sure why SOLVED!

I’m just not sure why this isn’t meeting that one test where we have to create an array if the prop is tracks and there isn’t already a tracks property and then push the value. I would really appreciate the help and advice. I’ve tried just putting prop after the [id] and it doesn’t seem to work. Thanks in advance!!

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

This attempts to push a value to collection[id].tracks even if it doesn’t exist.

1 Like

Gotcha!!! As soon as i posted this that’s what I started to look at! Thanks for the nudge in the right direction, I really appreciate it!!!

Glad to help. Happy coding!

1 Like

Got it solved!! Thank you again!!

Congratulations!

awesome, but what happens if suddenly one wants to do updateRecords(1945, "release_year", 1978)? would your code work? can you make it work, whatever value is passed as prop?

1 Like

It definitely wouldn’t work lol. Thanks for pointing that out!!! What, ultimately, would have been smarter is if I deleted my

} else if (prop == “album”) {
collection[id].album = value;
} else if (prop == “artist”) {
collection[id].artist = value; }

with

} else {
collection[id][prop] = value; }
I really hadn’t thought about something like that. In fact looking at my code now I see that I could shorten it even more lol

1 Like