freeCodeCamp Challenge Guide: Record Collection

freeCodeCamp Challenge Guide: Record Collection
0

Record Collection


Problem Explanation

You are given a JSON object representing (a small part of) your record collection. Each album is identified by a unique id number and has several properties. Not all albums have complete information.

Write a function which takes an id, a property (prop), and a value.

For the given id in collection:

If value is non-blank (value !== “”), then update or set the value for the prop.

If the prop is “tracks” and value is non-blank, check to see if the given element in the array has the property of “tracks”. If the element has the property of “tracks”, push the value onto the end of the “tracks” array. If the element does not have the property, create the property and value pair.

If value is blank, delete that prop.

Always return the entire collection object.

  • Change the code below // Only change code below this line and up to // Alter values below to test your code.
  • Take note that you are editing the inside of the updateRecords function.
  • For the given id parameter, which is associated to the collection 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 the collection object.

Hints

Hint 1

Use an else if statement to check the needed steps.

Hint 2

The second step listed in the instructions should be first in your else if statement.

Hint 3

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


Solutions

Solution 1 (Click to Show/Hide)
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;
}

Code Explanation

  • First checks if prop is equal to tracks AND if value isn’t a blank string. If both tests pass, value is pushed into the tracks array.
  • If that first check doesn’t pass, it next checks only if value isn’t a blank string. If that test passes, either a new key (prop) and value (value) are added to the object, or an existing key is updated if the prop already exists.
  • If both these checks fail (meaning value must be an empty string), then the key (prop) is removed from the object.

Example Run

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

Relevant Links

3 Likes

This challenge should be in Intermediate algorithm part imho. It was super easy! Can we contribute to the structure of curriculum?

2 Likes

I feel like you can write it with just simple binary conditionals

if you start by checking if the value is blank, then you wouldn’t have to use && value!="" in the rest of your conditions.

if(prop == "tracks" && !collection[id].hasOwnProperty("tracks")){collection[id].tracks = []; collection[id].tracks.push(value);}
  if(prop !== "tracks" && value !== ""){ collection[id][prop] = value; }
  if(prop == "tracks" && value !== ""){collection[id].tracks.push(value);}
  if(value === ""){delete collection[id][prop];}
  

Looks like I could have made this more efficient; but it works and is legible.

function updateRecords(id, prop, value) {
  
  if(prop !== 'tracks' && value !== "") {
    collection[id][prop] = value;
  }
  
  if(prop === 'tracks' && collection[id][prop] === undefined) {
    collection[id][prop] = [];
  }
  
  if(prop === 'tracks') {
    let track = collection[id][prop];
    track.push(value)
  }
  
  if(value === "") {
    delete collection[id][prop]
  }
  
  console.log(collection)
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
updateRecords(5439, "tracks", "Take a Chance on Me");
updateRecords(2468, "tracks", "Free");
3 Likes

My answer looked pretty clean (I guess…). Any constructive critics are welcome…

function updateRecords(id, prop, value) {
  if (value === '') {
    delete collection[id][prop];
  } else {
    if (prop !== 'tracks') {
      collection[id][prop] = value;
    } else {
      collection[id].hasOwnProperty('tracks') ? collection[id].tracks.push(value) : collection[id].tracks = [value];
    }
  }
  return collection;
}
14 Likes

I wrote value.length!==0 instead value!==""

1 Like