Record Collection

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, add the value to the end of the album’s tracks array. You need to create this array first if the album does not have a tracks property.

Note: A copy of the recordCollection object is used for the tests. You should not directly modify the recordCollection object.

わかってる方ぜひお教えてください。

const 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'
  }
};


function updateRecords(records, id, prop, value) {
 if(value === ""){
   delete records[id][prop];
 }else if(prop!=='tracks' && value !== "" ){
   records[id][prop]=value;
 }else if(prop==='tracks' && value !== "" ){
   if(!records[id].hasOwnProperty('tracks')){
   records[id][tracks] = value;  
 }else{
   records[id][tracks].push(value);
    } 
 }
  return records;
}

どこが間違っているか、わかりません。
ぜひお教えてください。

Thank you very much.

This is not right:

function updateRecords(records, id, prop, value) {
 if(value === ""){
   delete records[id][prop];
 }else if(prop!=='tracks'){
   records[id][prop]=value;
 }else if(prop==='tracks'){
   if(!records[id].hasOwnProperty('tracks')){
   records[id][prop] ="";  
 }
   records[id][tracks].push(value);  
 }
  return records;
}

What does this line do?

Thank you very much.

“ You need to create this array first if the album does not have a tracks property.

so I want to create a “tracks”.

Right. But “” is not an array.

Thanks for your help…

so I think it is : records[id][prop] =;
but,it dose’t work.

One more issue. Do you have a variable named tracks or do you want a property called "tracks"

Thank you very much. 誠にありがとうございました。
I got it.

function updateRecords(records, id, prop, value) {
if(value === “”){
delete records[id][prop];
}else if(prop!==‘tracks’){
records[id][prop]=value;
}else if(prop===‘tracks’){
if(!records[id].hasOwnProperty(‘tracks’)){
records[id][prop] = ;
}
records[id][prop].push(value);
}
return records;
}

2 Likes

I’m having issues with the same problem. Here is my code. It’s identical to the one above by yyttnom:

function updateRecords(records, id, prop, value) {
if (value === “”) {
delete records[id][prop];
}
else if (prop !== “tracks”) {
records[id][prop] = value;
}
else if (prop === “tracks”) {
if (!records.hasOwnProperty(“tracks”)) {

  records[id][prop] = [];
  if (records.hasOwnProperty("tracks")) {
     records[id][prop].push(value); 
  }
}

records[id][prop].push(value); 

}

return records;
}

The issue is with:
records[id][prop] = ;

This seems to be setting the tracks value to instead of only setting it to when it doesn’t exist. How can resolve this?

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.