I Need Help With The Record Collection Problem

I Need Help With The Record Collection Problem
0

So… I can’t understand what I did wrong here :frowning:
I can’t pass this:
“After updateRecords(5439, "tracks", "Take a Chance on Me") , tracks should have "Take a Chance on Me" as the last element.”

// 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 (prop !== "tracks" && value !== "") {

    collection[id][prop] = value;

  } else if (prop === "tracks" && value !== "") {

    collection[id].tracks.push(value);

  } else if (value === "") {

    delete collection[id][prop];

  } 

    return collection;
This text will be hidden
  

}

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

I believe you forgot this condition:

If prop is "tracks" but the album doesn’t have a "tracks" property, create an empty array before adding the new value to the album’s corresponding property.

@JeremyLT How can I correct this?

if (prop === "tracks" && collection[id][prop] === undefined) {

  collection[id][prop] = [];

}

I wrote this code, but did not resolve the problem so far

You have the right logic in this case… almost.

  } else if (prop === "tracks" && value !== "") {

    collection[id].tracks.push(value);

The problem is that if you don’t have a tracks, then you can’t push onto it. I would add some logic here to make sure that you have tracks before you push or to otherwise handle the special case of there not being an array to push onto yet.

1 Like

Got it. I did some modifications and it worked

The final resolution:

if (value === "") {

    delete collection[id][prop];

  }

   else if (prop === "tracks") {

    collection[id][prop] = collection[id][prop] || [];

    collection[id][prop].push(value);

  } else if (prop === "tracks" && value !== "") {

    collection[id].tracks.push(value);

  } else if (prop !== "tracks" && value !== "") {

    collection[id][prop] = value;

  }

Thank You! :grinning:

Awesome! Nice work fixing the tricky case!