FCC Record Collection

This challenge had to be one of the most frustrating ever. Heres the setup

Record Collection

You are given a JSON object representing (a small part of) your record collection. Each album is identified by a unique id number and has several properties. Not all albums have complete information.

Write a function which takes an id, a property (prop), and a value.

For the given id in collection:

If value is non-blank (value !== "") and prop is not "tracks" then update or set the value for theprop.

If the prop is "tracks" and value is non-blank, push the value onto the end of the tracks array.

If "tracks" is non-existent before you update it, create an empty array before pushing a track to it.

If value is blank, delete that prop.

Always return the entire collection object.

Now here is the code they give you to work with:
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: [
“Little Red Corvette”
1245: {
artist: “Robert Palmer”,
tracks: [ ]
5439: {
album: “ABBA Gold”
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

function updateRecords(id, prop, value) {

return collection;

// Alter values below to test your code
updateRecords(2548, “tracks”, “take”);

Now I had all the right ideas going into it but oh man. This one has a twist that will throw you for a loop and after you solve it you still won’t get why unless you are a computer scientist with 20 plus years experience lol. And the reason I say this is because hey after an hour of refactoring my code I broke down and searched for the solution. Now here’s the twist…. dun dun dun. None of those solutions are right due to another condition being added on to the challenge recently LOL. But they still pointed me in the right direction and it also taught a great lesson about refactoring my code and making it cleaner. There are a lot of talented programmers out there! So here’s a setup I found that was right but still Wrong.

if (!value) delete collection[id][prop];
if(prop == “tracks”) collection[id].tracks = [];
if (!!value && prop === ‘tracks’)collection[id].tracks = [value];
if (!!value && prop === ‘artist’) collection[id][prop] = value;

Now if you read back to the conditions you’ll see that if the prop value is “tracks” and theirs no value it should delete the prop. But at the same time it should create a blank array. LOL Do you see my problem here. So let the brain storming begin. I looked at the code setup as you can see the delete statement is nested at the very top so if theirs a prop with no value it will delete it automagically. So after 10 minutes of thinking and moving things around I said let me put that same delete statement next to the second if statement so when it creates a “tracks” array it will also delete it. and wallaaa. It worked!!!!!!!! But here is the worst part about this answer if there were tracks in the object already and “tracks” does have a value all the tracks stored previously get deleted and guess what the code still works… Can anyone say BUG!!!!! Anyways

So here’s the solution for all the campers having the same problem. I hope you read search and ask first as I did but also make sure you learn from every answer you take

if (!value) delete collection[id][prop];
if(prop == “tracks”) collection[id].tracks = []; delete collection[id][prop];
if (!!value && prop === ‘tracks’)collection[id].tracks = [value];
if (!!value && prop === ‘artist’) collection[id][prop] = value;

Books referenced during all this madness were:

JavaScript The Good Parts

Eloquent JavaScript

You don’t know JavaScript: This & Object prototypes



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s