freeCodeCamp Challenge Guide: Record Collection

Record Collection


Problem Explanation

For the given id parameter, which is associated to the 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 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 object[id][prop].


Solutions

Solution 1 (Click to Show/Hide)
var collection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

// Only change code below this line
function updateRecords(object, id, prop, value) {
  if (value === '') delete object[id][prop];
  else if (prop === 'tracks') {
    object[id][prop] = object[id][prop] || [];
    object[id][prop].push(value);
  } else {
    object[id][prop] = value;
  }

  return object;
}

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.
  • 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

36 Likes

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

21 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.

8 Likes
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];}
  
32 Likes

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");
43 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;
}
63 Likes

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

13 Likes