Record Collection (Why are the brackets needed for [value] when updating an object property?)

Tell us what’s happening:

I’m confused as to why FCC is requiring the parameter value to be in brackets in the second level of the IF statement but not in the first level?

When I run the updateRecords function without using brackets in either levels of the IF statement through a Google Chrome snippet, it works fine.

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

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": [ 
        "1999", 
        "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" && value !== "") {
    if (collection[id][prop]) {
        collection[id][prop].push(value);
    } 
    else {
        collection[id][prop] = [value];
    }
  } else if (value !== "") {
    collection[id][prop] = value;  
  } else {
    delete collection[id][prop];
  }
  
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
updateRecords(5439, "tracks", "Take a Chance on Me");
updateRecords(2548, "artist", "");
updateRecords(1245, "tracks", "Addicted to Love");
updateRecords(2468, "tracks", "Free");
updateRecords(2548, "tracks", "");
updateRecords(1245, "album", "Riptide")

the tracks key should always return an array, so if tracks doesnt exist, it will create a key called tracks and give a new value of [value]

1 Like

I totally missed that detail. Thanks, Dominic!