Record collection - removing properties

Tell us what’s happening:
For this problem I was able to get the code to change values and add properties where required according to the test problems for the code. However for the removal questions where I am supposed to terminate a property if that value is blank, it says I am not doing it correctly. So when I did a console log test (which can be seen above “my code so far”) I get a result which to me indicates that I have removed tracks and am therefore confused as to how I have not solved the problem. Can anyone help me understand if I have made a misunderstanding with the question, or if I have another step i need to do to complete the solution.

{ '1245': { artist: 'Robert Palmer', tracks: [] },
  '2468': 
   { albumTitle: '1999',
     artist: 'Prince',
     tracks: [ '1999', 'Little Red Corvette' ] },
  '2548': { albumTitle: 'Slippery When Wet', artist: 'Bon Jovi' },
  '5439': { albumTitle: 'ABBA Gold' } }
  **Your code so far**

// Setup
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'
  }
};

// Only change code below this line
function updateRecords(records, id, prop, value) {

// checks if value is empy 
  if (value != "") {
  // for when looking at artists
    if (prop != "tracks") {
      if (!recordCollection[id].hasOwnProperty("artist")) {
        recordCollection[id][prop] = value;
      } else {
        recordCollection[id][prop] = value;
      }
  }

  // for when looking at tracks
  if (prop == "tracks") {
    if (recordCollection[id].hasOwnProperty("tracks")) {
      recordCollection[id][prop].push(value);
    } else {
      recordCollection[id][prop] = [value];
    }
  } 
  } else {        // removes properties with no values 
    delete recordCollection[id][prop];
  }
  return records = recordCollection;
}

console.log(updateRecords(recordCollection, 2548, "tracks", ""));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Record Collection

Link to the challenge:

It would help us see your logic if you used consistent indentation at each logical level.

You should never reference the global variable recerdCollection in your code. In general you should just avoid directly referencing global variables.

OK, sorry for the indentation, it got messy when I was trying to figure out solutions. It should be fixed now.

What should I do to return the recordCollection instead then?

As far as your function knows (ideally), recordCollection does not exist. You only have the four function arguments records, id, prop, and value.

1 Like

Thank you so much, that opened my eyes there, it is now solved.

1 Like