Record Collection - Help!

Tell us what’s happening:
Ok, I’m stumped. I’ve searched this forum and google. I’ve read the replies and I still don’t understand why my code isn’t working on this exercise.

The only test I fail is the second one, " After updateRecords(5439, “tracks”, “Take a Chance on Me”),tracksshould have"Take a Chance on Me"` as the last element."

So the problem MUST be in the code that attempts to create the “tracks” property when it doesn’t exist. But despite knowing that, I cannot figure out how to fix it.

Any assistance would be greatly appreciated.

Your code so far

// Setup
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));

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

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0.

Link to the challenge:

When your solution is run with updateRecords(5439, “tracks”, “Take a Chance on Me”), the tracks value is not an array. Can you see where your solution is assigning a string instead of an array?

1 Like

Hmmmm. Well that fixed it but I don’t understand why. I changed ‘collection[id][prop] = value’ to ‘[value]’.

Why doesn’t that make an array inside of an array since the preceding line is ‘collection[id].tracks = ’ ?

Because you then overwrite it with just value. The previous value has no bearing.

someVar = 1;
// someVar is now 1
someVar = 2;
// someVar is now 2
someVar = [];
// someVar is now []
someVar = 'hello';
// someVar is now 'hello'
1 Like

I think I was confused by the instructions, which state " If prop is "tracks" but the album doesn’t have a "tracks" property, create an empty array before adding the new value to the album’s corresponding property."

Apparently, you’re not supposed to do that at all - in fact, it messes you up if you do it!

Well, not really - you can simply do:

/* if the record doesn't have a tracks property... */
collection[id].tracks = [];

/* and then in EITHER case... */

But the collection[id].tracks = [value]; is a shorthand way of creating an array AND inserting that first value, in a single line. Be aware of both ways, but don’t feel bound to one or the other.

I also tried the code you suggested to create the empty array and got an error.

Some random error, or?

Here’s something to consider, the logic might be better changed a little:

  • if prop is ‘tracks’ and value is not empty
    • if collection[id] does not have a tracks property
      • create an empty tracks property, exit the inner if statement
    • Here, whether we had one or not, we now have a tracks property. Add the value!

Rather than overloading the if statements, have a single, simple if, then within that, handle your possible branches for that particular case.

Thinking about it, it was probably still the value vs [value] error. The error log is not very specific on those challenges.

1 Like