Record Collection challenge not working

Tell us what’s happening:
Have tried several different ways to pass this challenge. Have looked at several other peoples codes and the accompanying video but have no success.

Your code so far


// 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) {
// 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');

Your browser information:

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

Challenge: Record Collection

Link to the challenge:

Hi and welcome to the forum!

You aren’t using the object passed in and are instead directly referencing the global object. You should be using the object passed in.

1 Like

So it should end with
return collection[id][prop];

That’s the other way around. You need to use object instead of the global variable collection.

did return collection and got these 2 errors left.

After

updateRecords(collection, 2548, "artist", "")

,

artist

should not be set After

updateRecords(collection, 2548, "tracks", "")

,

tracks

should not be set // tests completed

You should not return collection.

Per the instructions:

Complete the function using the rules below to modify the object passed to the function.
Your function must always return the entire object.

return updateRecords;

What is your current full code? I don’t understand your last post.

// 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'

  }

};

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 ?;

}

You are using collection inside of your function. You should not be doing this. You should instead be using the object. Once you have modified the object inside the function, then you need to return the object to pass the tests.

So instead of generic(global) use of collection, i should be using that particular objects id or prop?

yes, but no

instead of the specific global object collection that is in the editor, you need to use the function parameter object to make the function reusable, as that will have value of whatever argument the function is called with

1 Like

Ok i got it, instead of “collection” i used “object” and it passed. Thanks guys!

1 Like

I got it, i changed collection to object and it passed then was curious and changed all objects to collections and it passed, thanks for your help.