Record Collection Code Explanation

Hey guys,

I’ve been so confused the whole day by the Record Collection challenge in the JavaScript section. I ended up using the hints, trying some more, searching on here etc but ultimately had to check the solution before trying again, and still I had to use the solution again to fix it!

Here’s what I have. Could somebody explain why, within the two lines of “collection[id][prop] = [value];”, one must have square brackets and the other not? I was trying for so long before discovering that having square brackets on both or neither would fail the test and I can’t see why.

function updateRecords(id, prop, value) {
if (prop == “tracks” && value !== “”) {
if (collection[id][prop]) {
collection[id][prop].push(value);
} else {
// v This line v
collection[id][prop] = [value];
}
} else if (prop !== “tracks” && value !== “”) {
// v And this line v
collection[id][prop] = value;
} else {
delete collection[id][prop];
}
return collection;
}

Thanks to anyone who can clear this up for me :slight_smile:


// 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 {
// v This line v
      collection[id][prop] = [value];
    }
  } else if (prop !== "tracks" && value !== "") {
// v And this line v
    collection[id][prop] = value;
  } else {
    delete collection[id][prop];
  }
  return collection;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 10718.88.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.118 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection

This is a value in an array:

[1]

This is a value not in an array:

1

[value] isn’t some special syntax, just having value fails because that’s not the right thing, it’s supposed to be in an array.

That makes a lot of sense, but how come the last line here doesn’t require square brackets?

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

Basically the way I am thinking of it (is probably wrong):

‘if there is already a property called “tracks”, push “value” to the end of “tracks”, else create a property called “tracks” and assign it [value]’

Is it just because we can put a non-array item into the “tracks” array, however when creating a “tracks” property, the challenge itself won’t accept a non-array “tracks” property?

You can put whatever you like into tracks, but because tracks is supposed to be an array of track names, anything else will fail :wink:

This is correct, if you’re using push, then the tracks array already exists (and if you only had the push and didn’t account for tracks maybe not being there, the function would error out if it tried to push to a non-existant thing)

1 Like

You know, that actually happened a lot to begin with. I totally blanked that .push() only works in an array!

Thanks a lot :slight_smile:

1 Like