Record collection

These are the objectives:

  • After updateRecords(recordCollection, 5439, “artist”, “ABBA”), artist should be the string ABBA

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

  • After updateRecords(recordCollection, 2548, “artist”, “”), artist should not be set

  • After updateRecords(recordCollection, 1245, “tracks”, “Addicted to Love”), tracks should have the string Addicted to Love as the last element.

  • After updateRecords(recordCollection, 2468, “tracks”, “Free”), tracks should have the string 1999 as the first element.

  • After updateRecords(recordCollection, 2548, “tracks”, “”), tracks should not be set

  • After updateRecords(recordCollection, 1245, “albumTitle”, “Riptide”), albumTitle should be the string Riptide

This is my code:

// 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) {
  if (prop !== "tracks" && value.length !== 0) {
    recordCollection[id][prop] = value;
  } else if ( prop == "tracks" && recordCollection[id].hasOwnProperty("tracks") == false) {
    recordCollection[id].tracks = [];
    recordCollection[id].tracks.push(value);
  } else if ( prop == "tracks" && value.length > 0) {
    recordCollection[id][prop].push(value);
  } else if ( value.length < 1) {
    delete recordCollection[id][prop];
  }
  return records;
}

updateRecords(recordCollection, 5439, "artist", "ABBA");
updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me");
updateRecords(recordCollection, 2548, "artist", "");
updateRecords(recordCollection, 1245, "tracks", "Addicted to Love");
updateRecords(recordCollection, 2468, "tracks", "Free");
updateRecords(recordCollection, 2548, "tracks", "");
updateRecords(recordCollection, 1245, "albumTitle", "Riptide");

console.log(recordCollection);

This is what the console logs:

// console output
{ '1245': 
   { artist: 'Robert Palmer',
     tracks: [ 'Addicted to Love' ],
     albumTitle: 'Riptide' },
  '2468': 
   { albumTitle: '1999',
     artist: 'Prince',
     tracks: [ '1999', 'Little Red Corvette', 'Free' ] },
  '2548': { albumTitle: 'Slippery When Wet' },
  '5439': 
   { albumTitle: 'ABBA Gold',
     artist: 'ABBA',
     tracks: [ 'Take a Chance on Me' ] } }

But, freeCodeCamp recognizes my solution as incorrect. Can anyone please help me with this? I’m new to coding.

I was expecting to get to the next level in freeCodeCamp, bc it appears as though my solution works.

You should not reference variables outside the scope of your function.

1 Like

thanks man! I appreciate it. That makes a lot of sense!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.