Record Collection - Code Help

This code does not pass the test, so I could use a second pair of eyes on it;

The console output looks correct based on the challenge requirements as follows:
5439 - Update artist to ‘Abba’ and add track ‘Take a Chance on Me’
2548 - Remove artist ‘Bon Jovi’ and tracks
1245 - Add track ‘Addicted to Love’ and albumTitle of ‘Riptide’
2468 - Add track ‘Free’ and track ‘1999’ is first in the array.

Console output is first with the code following:

Advance thanks for any direction you can provide!!


// 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' ] } }
{ '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' ] } }

// CODE
// Setup
var collection = {
  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(object, id, prop, value) {
  if (value === '') {
    delete collection[id][prop];
  } else if (prop === 'tracks') {
    collection[id][prop] = collection[id][prop] || [];
    collection[id][prop].push(value);
  } else {
    collection[id][prop] = value;
  }
  return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');
updateRecords(collection, 5439, 'tracks', 'Take a Chance on Me');
updateRecords(collection, 1245, 'tracks', 'Addicted to Love');
updateRecords(collection, 2468, 'tracks', 'Free');
updateRecords(collection, 2548, 'tracks', '');
console.log(updateRecords(collection, 1245, 'albumTitle', 'Riptide'));

when there are function parameters, you need to use them instead of referencing global objects

also, note you are using collection all through your function but then return object which was probably unchanged if it’s a different object that is being passed in

1 Like

Thanks so much for reaching out, Magical Girl!!

I have changed the return statement to reflect:

return collection;

I also removed ‘collection’ from the updateRecords statement.

updateRecords(collection, 5439, 'artist', 'ABBA');

I have to be missing something obvious because the code still does not pass muster.

Feeling dam-braimaged . . .

I’ve been fighting with this in Visual Studio Code, and here is the console output, which looks right:

1. {1245: {…}, 2468: {…}, 2548: {…}, 5439: {…}}

  1. 1245:

    1. albumTitle: "Riptide"
    2. artist: "Robert Palmer"
    3. tracks: ["Addicted to Love"]
    4. __proto__: Object

  2. 2468:

    1. albumTitle: "1999"
    2. artist: "Prince"
    3. tracks: (3) ["1999", "Little Red Corvette", "Free"]
    4. __proto__: Object

  3. 2548:

    1. albumTitle: "Slippery When Wet"
    2. artist: "Bon Jovi"
    3. __proto__: Object

  4. 5439:

    1. albumTitle: "ABBA Gold"
    2. artist: "ABBA"
    3. tracks: ["Take a Chance on Me"]
    4. __proto__: Object

  5. __proto__: Object

new code statement below:

// Setup
var collection = {
  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'
  }
};

// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  if (value === '') {
    delete collection[id][prop];
  } else if (prop === 'tracks') {
    collection[id][prop] = collection[id][prop] || [];
    collection[id][prop].push(value);
  } else {
    collection[id][prop] = value;
  }
  return collection;
}

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

console.log(collection);

Hello!

Your initial code was right :stuck_out_tongue:!

As explained by @ieahleen, in your original code you must change collection to object inside your function, that’s all :slight_smile:.

This code:

Will not pass the tests because you modified the function signature, which needs to be:

function updateRecords(object, id, prop, value) {
}

Grazie!! Most and much appreciated!!

Another question, my friend.

I seem to have everything working in the code except for the statements except for those pertaining to record #2448.

At a loss of what I could be missing now that I have:

  1. Corrected the updateRecords statements
  2. Set the return statement to ‘collection’

The result set seems to reflect what it should for 2548.

Can you help a dam-braimaged brother out?

Thanks!!

// running tests
After updateRecords(collection, 2548, "artist", ""), artist should not be set
After updateRecords(collection, 2548, "tracks", ""), tracks should not be set
// tests completed
// 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' ] } }
{ '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' ] } }

I used the following code:

// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(object, id, prop, value) {
  if (value === '') {
    delete collection[id][prop];
  } else if (prop === 'tracks') {
    collection[id][prop] = collection[id][prop] || [];
    collection[id][prop].push(value);
  } else {
    collection[id][prop] = value;
  }
  return collection;
}

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

console.log(collection);

AHA!!!

I finally figured this one out:

In the function block, I passed the global variable of “collection” instead of passing the function variable “object”. I get it now!!

What a “duh” moment . . . they do say the mind is the first thing to go, or was that something else???

Thanks to ALL who provided assistance, I get by with a lot of help from my friends!!!

Cheers! Issue solved!!

1 Like