Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I put this in the Record Collection in FreeCodeCamp assignment but I couldn't really figure it out. Could you tell where I was going wrong?

The instructions for this assignment are provided below:
___________________________________________________________________
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.
______________________________
One of the tests are:
After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"), tracks should have the string Take a Chance on Me as the last and only element.

What I have tried:

<pre>function updateRecords(records, id, prop, value) {
    if (value == '') {
        delete records[prop]; 
        return records;  
    } else if (prop !== 'tracks' && value !== ""){
        value = records[id][prop]; 
        return records; 
    } else if (prop == 'tracks' && value != ""){
    if (records[id].hasOwnProperty('tracks')){
records[id] ['tracks'] = records[id] ['tracks'] + "," + value; 
    } else {
    records[id]['tracks'] = value;
    } return records;
  }};
  
  updateRecords(recordCollection, 5439, 'artist', 'ABBA');
Posted
Updated 31-Jul-23 18:46pm

1 solution

Read the instructions again, carefully this time:
Quote:
If prop isn't tracks and value isn't an empty string, assign the value to that album's prop.
} else if (prop !== 'tracks' && value !== ""){
    value = records[id][prop];
Isn't that assignment the wrong way round?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900