Help with code - Basic JS: Record Collection

Hi everyone,

I’m currently on a weird exercise on the Javascript tutorial where you have to create an update music records function. Here is the Link.

When I tried to push an album to an existing track, it deletes the tracks that already exists. I tried following the if/else cases but I can’t find what’s wrong. I was so happy that I got everything right besides this edge case…

// Basic JavaScript: Record Collection
var collection = {
    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(object, id, prop, value) {
    object[id] = {};
    if (prop != "tracks" && value != '') {
        object[id][prop] = value;
    }
    else if (value === '') {
        delete object[id][prop];
    }
    else if (prop === "tracks" && object[id][prop] != undefined) {
        object[id][prop].push(value)
    }
    else if (prop === "tracks" && object[id][prop] === undefined) {
        object[id][prop] = []; 
        object[id][prop].push(value);
    }
    return object;
  }
  
updateRecords(collection, 2468, "tracks", "Free")

Remove this line and try it again :slight_smile:

1 Like

Hi and welcome to the forum!

What is this line for?

1 Like

No friggen way! Can’t believe one small line of code messed up the solutions. Thank you so much!!

hahaha, thank you for the warm welcome!

Originally the output gave “id”, “prop”, “value” and not the actual names. So I thought let me set object[id] = {} to create an empty object inside collections. I still don’t know how everything worked besides that edge case…

I love coding. A semi-colon can kill you.