Problem with 'Record Collection' challenge

Hi !
I don’t really understand what’s wrong here… I’ve tried console.log(updateRecords(recordCollection, 5439, “tracks”, “Take a Chance on Me”)), and it matches what is expected.
So, can you help me and explain why it keeps saying “After ‘updateRecords(recordCollection, 5439, “tracks”, “Take a Chance on Me”)’ , ‘tracks’ should have the string ‘Take a Chance on Me’ as the last element.”
Thanks in advance !


// Setup
var 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'
}
};

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

updateRecords(recordCollection, 5439, 'artist', 'ABBA')

Challenge: Record Collection

Link to the challenge:

Hello!
You made it so that the album with the id 5439 has a property called “tracks” which is a string. If you read the challenge, you will see that it wants the last element in “tracks” to be the correct song. You should in other words make sure the song is added to an array to the property “tracks”.

It has to do with the following in your code:

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

You should make sure the above code doesn’t make recordCollection[5439].tracks a string but instead an array with just one element (the track string).

Does that make sense? :slight_smile:

Yes, I think it makes sense, thank you so much for your anwer . I corrected records[id][“tracks”] = [value]; and it worked.
But is records[id][prop] = [value]; the actual correct answer ?

That is one way yes :slight_smile:

One thing I notice, however, is that an empty value would mean that you delete the given property, but if you were to provide a property that is not yet declared but give it an empty value, you will with your current solution create the property without a proper value.

I would consider putting the "if (value == "") {..." statement up earlier in the if-else code block. Also because if you first check that (as the first if-statement), you will not have to constantly check “(... && value != "")” in the other statements.

But if it works and passes the challenges then you don’t need to make any changes unless you want to clean up your code and improve your debugging skills :wink:

Ah ok, I see. It’s more elegent and also make more sense. I guess I was too focus on the requirements, and not enough on the big picture.
Thanks again for taking the time to help !

1 Like

When you create records[id].tracks this way, it is not an array.

1 Like

Of course, always happy to help :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.