Build a Record Collection - Build a Record Collection

Tell us what’s happening:

I need assistance in making my function work properly. I can not complete Tests 2-8 and am unsure on what is wrong with my function code. Help would be much appreciated.

Your code so far

const recordCollection = {
  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'
  }
};


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

console.log(updateRecords(recordCollection, 5439, "artist", "ABBA"))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0

Challenge Information:

Build a Record Collection - Build a Record Collection

It looks like you haven’t finished your function

I added the statement but it still doesn’t work.

What did you add? What exactly “doesn’t work”?

To my function I added

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

The function will still not work properly, and I am not sure what the error is.

What exactly does “not work properly” mean though?

Note - you are not meeting user story 1

Here is my updated code



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


I only need to complete tests 3 and 6. I am not sure what the issue in my code is so help is appreciated.

Did you try running the failing test yourself manually?

console.log(updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"))

When I run that first failing test, I see something important in the console.

Alright I solved it. My issue was in my if blocks and my .hasOwnProperty() method. This was my code:

if(prop != "tracks" && value != "") {
      records[id][prop] = value;
  } if(prop == "tracks" && value != "" && records[id].hasOwnProperty("tracks") != undefined) {
      records[id][prop] = [value];

My new code is:

else if (prop !== "tracks") {
    records[id][prop] = value;
  } else {
    // prop is "tracks" and value isn't empty
    if (!records[id].hasOwnProperty("tracks")) {
      records[id][prop] = [];

So I fixed it. Thanks for your hints!

1 Like

Nice work. I figured with that log you’d be able to cross the finish line!