freeCodeCamp Challenge Guide: Record Collection

Record Collection


Problem Explanation

For the given id parameter, which is associated with the records object:
* If the value parameter isn’t an empty string, update (or set) the value parameter for the prop parameter.
* If the prop parameter is equal to "tracks" and the value isn’t an empty string, push the value onto the end of the tracks array.
* If value is an empty string, delete that prop from the object.

  • Finally, return records.

Hints

Hint 1

Use an if...else if chain to check the needed steps.

Hint 2

To access the value of a key in this object, you will use records[id][prop].

Hint 3

You can’t push to an array that doesn’t exist. Use hasOwnProperty to check first.


Solutions

Solution 1 (Click to Show/Hide)
function updateRecords(records, id, prop, value) {
  if (prop !== 'tracks' && value !== "") {
    records[id][prop] = value;
  } else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
    records[id][prop] = [value];
  } else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value);
  } else if (value === "") {
    delete records[id][prop];
  }
  return records;
}
Solution 2 (Click to Show/Hide)
function updateRecords(records, id, prop, value) {
  if (value === '') {
    delete records[id][prop];
  } else if (prop === 'tracks') {
    records[id][prop] = records[id][prop] || []; // this is called shortcircuit evaluation, see below for explanation
    records[id][prop].push(value);
  } else {
    records[id][prop] = value;
  }
  return records;
}

Code Explanation

  • First checks if value is a blank string. If so, then the key (prop) is removed from the object.
  • If that first check doesn’t pass, it next checks if prop is equal to tracks. The tracks array is initialized if it does not exist, and then value is pushed into the tracks array. (This step uses shortcircuit evaluation)
  • If both these checks fail (meaning value is not an empty string and prop is not tracks), then either a new key (prop) and value (value) are added to the object, or an existing key is updated if the prop already exists.

Example Run

  • updateRecords(5439, "artist", "ABBA"); runs.
  • value is not a blank string, so the first condition of the else if statement fails.
  • prop is equal to “artist”, not “tracks”, so the second condition of the else if statement fails.
  • in the ‘else’ clause, artist: "ABBA" is added to the 5439 id.

Relevant Links

Solution 3 (Click to Show/Hide)
function updateRecords(records, id, prop, value) {
  // Access target album in record collection
  const album = records[id];

  // If value is an empty string,
  //  delete the given prop property from the album
  if (value === "") {
    delete album[prop];
  }
  // If prop isn't tracks,
  //  update or set that album's prop to value
  else if (prop !== "tracks") {
    album[prop] = value;
  }
  // If prop is tracks,
  //  add value to the end of the album's existing tracks array
  else {
    album["tracks"] = album["tracks"] || [];
    album["tracks"].push(value);
  }

  // Your function must always return the entire record collection object
  return records;
}

This solution uses the fact that objects are stored as references to slightly simplify the solution syntax.

243 Likes