Skip to content

Instantly share code, notes, and snippets.

@arnonrdp
Last active August 8, 2023 08:40
Show Gist options
  • Save arnonrdp/06b4a3e3f3a6a4de09736aaaa8f170f9 to your computer and use it in GitHub Desktop.
Save arnonrdp/06b4a3e3f3a6a4de09736aaaa8f170f9 to your computer and use it in GitHub Desktop.
freeCodeCamp - JavaScript Algorithms and Data Structures - Record Collection
// Setup
var recordCollection = {
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(records, id, prop, value) {
if (value === "") {
delete records[id][prop];
} else if (prop === "tracks") {
records[id][prop] ??= [];
records[id][prop].push(value);
} else {
records[id][prop] = value;
}
return records;
}
updateRecords(recordCollection, 5439, "artist", "ABBA");

Record Collection

You are given an object literal representing a part of your musical album collection. Each album has a unique id number as its key and several other properties. Not all albums have complete information.

You start with an updateRecords function that takes an object literal, records, containing the musical album collection, an id, a prop (like artist or tracks), and a value. Complete the function using the rules below to modify the object passed to the function.

  • Your function must always return the entire record collection object.
  • If prop isn't tracks and value isn't an empty string, update or set that album's prop to value.
  • If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it.
  • If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array.
  • If value is an empty string, delete the given prop property from the album. Note: A copy of the recordCollection object is used for the tests.
@Suriya-pradath
Copy link

Ff

@u19192372
Copy link

hello, can you give a brief explanation of the code

@arnonrdp
Copy link
Author

Hello @u19192372
It's a lot of logical reasoning.

  • The first if checks if there is any value being passed as a function parameter. If not, we delete the prop that was passed;
  • The second if checks if the third parameter is tracks. Here we already know that some value has been passed, as it has already passed the first if. Then:
    • I use Logical nullish assignment (??=) to check if the tracks array already exists. If so, he moves on. If it doesn't exist, it creates it as an empty array;
    • Then I just insert the value passed into this array;
  • Finally (if a value was passed and if prop is not tracks) I just update the value of the prop.

@acmekrish
Copy link

Brilliant

@nadeTheBuggy
Copy link

Freecodecamp had changed the structure of the question up and im very confused now. This is my 5th day coding and this question is really making me wanna give up

Record Collection
You are creating a function that aids in the maintenance of a musical album collection. The collection is organized as an object that contains multiple albums which are also objects. Each album is represented in the collection with a unique id as the property name. Within each album object, there are various properties describing information about the album. Not all albums have complete information.

The updateRecords function takes 4 arguments represented by the following function parameters:

records - an object containing several individual albums
id - a number representing a specific album in the records object
prop - a string representing the name of the album’s property to update
value - a string containing the information used to update the album’s property
Complete the function using the rules below to modify the object passed to the function.

Your function must always return the entire records object.
If value is an empty string, delete the given prop property from the album.
If prop isn't tracks and value isn't an empty string, assign the value to that album's prop.
If prop is tracks and value isn't an empty string, you need to update the album's tracks array. First, if the album does not have a tracks property, assign it an empty array. Then add the value as the last item in the album's tracks array.
Note: A copy of the recordCollection object is used for the tests. You should not directly modify the recordCollection object.

@arnonrdp
Copy link
Author

arnonrdp commented Aug 8, 2023

@nadeTheBuggy, the question is the same. They just changed the order of the conditions.

The if/else sequence I created is just to try and make the whole thing less complex.

Do not give up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment