Almost correct Record Collection code

Hello, I don’t understand why this code doesn’t work:

function updateRecords(id, prop, value) { if (value !== "") { if (prop !== "tracks") { collection[id][prop] = value; } else if (collection.hasOwnProperty("tracks") == true) { collection[id].tracks.push(value); } else { collection[id].tracks = [value]; } } else { delete collection[id][prop]; } return collection; }

The only test it fails is " After updateRecords(2468, "tracks", "Free") , tracks should have "1999" as the first element.", it replaces the original tracks array with a new one with “Free” as the only track, I do not understand why since if it already has a tracks array it should just add “Free” to the end because of this bit:

else if (collection.hasOwnProperty("tracks") == true) { collection[id].tracks.push(value)"

Thanks for your attention.

Edit: I don’t know how to indent it correctly, sorry.

1 Like

So in your code, you check if the tracks property exists, using

else if (collection.hasOwnProperty("tracks") == true)

… and that is where your issue lies. Can you see a problem with that? Does collection have a tracks property?

1 Like

Oh my god, I knew it was gonna be something dumb like that, thank you for the answer.

1 Like

Not dumb at all, it’s easy to get bogged down in the language and lose your place. Just needed a fresh pair of eyes.

1 Like