Record Collection Problem Set (4th requirement)

The code below works, I had to follow the answer key to answer this problem set.
Unfortunately, I still don’t quite understand what’s happening at the first else/if statement. It’s the fourth requirement of the problem set, and I don’t understand how this code is fulfilling the requirement.

The code in question:

else if (prop === “tracks”) {
records[id][prop] = records[id][prop] || ;
records[id][prop].push(value);
}

  **Your code so far**

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

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

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/98.0.4758.102 Safari/537.36

Challenge: Record Collection

Link to the challenge:

This is the problem with using someone else’s code.

Let’s back up and look at what the 4th (and 3rd) requirement is.

  • If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
  • If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.

Let’s snip out the portion you are talking about for a second and look


// Only change code below this line
function updateRecords(records, id, prop, value) {
  if (value === "") {
    delete records[id][prop];
  } else if (prop === "tracks") {
    // The instructions say:
    // if the album doesn't have a tracks property, create an empty array and add value to it

    // otherwise, add the value to the end of the album's existing tracks array

  } else {
    records[id][prop] = value;
  }
  return records;
}

Forgetting about the solution you looked at, how could you accomplish the comments I provided? Start simple, how can you accomplish the first part, ‘if the album doesn’t have a tracks property’? If you aren’t sure, which part of the instructions are confusing you?

This is how I would write the code, but when running the tests it fails the
“After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me") , tracks should have the string Take a Chance on Me as the last element”
part.

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

// 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" !== true)) {
    records[id][prop] = [""];
    records[id][prop].push(value);
  }
  else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value);
  }
  else if (value === "") {
    delete records[id][prop];
  }
  return records;
}

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

You are close there

I think you made a typo:

  else if (prop === "tracks" && records[id].hasOwnProperty("tracks") !== true) {

(Pro tip: you can use !condition instead of comparing condition !== true)

function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && value !== "") {
    records[id][prop] = value;
  } else if (prop === "tracks" && records[id].hasOwnProperty("tracks") !== true) {
    // hmmmmm.......
    records[id][prop] = [""]; // What does this do? Is this "an empty array"?
    records[id][prop].push(value);
  } else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value);
  } else if (value === "") {
    delete records[id][prop];
  }
  return records;
}

(Other pro tip: the solution you found put value === "" first so you didn’t have to keep repeating value !== "" again and again)

Once we fix that one spot where I made a comment, then we can circle back to a comparison with the solution you found.

Thank you so much! I see I put the parentheses in the wrong spot, and now my code is running correctly.

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

// 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") !== true) {
    records[id][prop] = [""];
    records[id][prop].push(value);
  }
  else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value);
  }
  else if (value === "") {
    delete records[id][prop];
  }
  return records;
}

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

Ooooh, we have a bad test because that really should not pass the tests

What does this line do? Does it do what the requirements say?

An empty array doesn’t have any elements, not even an empty string!


You can see this repeated line in two of your if-else clauses. This indicates there may be an opportunity for combining those cases.

How could you rewrite a single case that looks something like

  } else if (prop === "tracks") {
    // Handle a missing array here
    if ( /* ???  */) {
      // ...
    }
    records[i][prop].push(value);
  } else {
    // ...
  }

Doing that would bring you halfway to the answer you found!


I went ahead and added a bug report for the broken test:

Your code is close but really shouldn’t pass with that extra empty string in there.

I can’t seem to combine the two statements with a successful test. Here’s my code now:

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

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

  else if (value === "") {
    delete records[id][prop];
  }
  return records;
}

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

You are close. These are your ‘top level’ if-else clauses:

You need to either a) handle value === "" first or b) carry value !== ""

Perfect! I moved value === "" to the top of my ‘top-level’ if-else clauses, and the tests ran successfully.

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

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

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

Awesome! We can now answer your initial question :laughing:

These two chunks of code do the same thing!

The first one is explicit while the second relies upon something known as ‘short circuiting’.

So this line says “set records[id][prop] to itself if its value is truthy (an array is truthy while undefined in falsy) and set it to the empty array [] if it is not”.

1 Like

Thank you so much for your help! I’ll be sure to look into short circuiting, so that I can better understand other’s code. And I’ll watch out for typos more too :sweat_smile:

1 Like

I’m glad to have helped. You did all the heavy lifting!

Typos, tragically, happen to all of us, even those of us who have been coding for years :upside_down_face:

like see here for an example of a typo :stuck_out_tongue:

1 Like