Getting unexpected error with the record collection exercise

Hey there,

My code seems to be working in regards to adding ‘ABBA’ to the artist, getting rid of the ‘Bon Jovi’ as an artist, adding ‘Free’ as a track to Prince, adding ‘Addicted to Love’ as a track for Robert Palmer and the album ‘Riptide’, but it is not making the tracks for ‘Slippery When Wet’ (id 2548) have an empty array. It is working as far as I can tell, except creating a “tracks” property on the end of 2548… Which is confusing because it is creating a tracks property with a value for ABBA - 5439 (when you run it). The empty brackets on the other side of || should create the property tracks but with an empty array if there are no track values passed through. Any insight into why this is happening would be much appreciated.

// 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, 2548, "artist", "");
updateRecords(collection, 1245, "tracks", "Addicted To Love");
updateRecords(collection, 2468, "tracks", "Free");
updateRecords(collection, 2548, "tracks", "");
console.log(updateRecords(collection, 1245, "albumTitle", "RipTide"))





Here is the output when I run the tests;

// running tests
After updateRecords(collection, 5439, "artist", "ABBA"), artist should be the string ABBA
After updateRecords(collection, 5439, "tracks", "Take a Chance on Me"), tracks should have the string Take a Chance on Me as the last element.
After updateRecords(collection, 2548, "artist", ""), artist should not be set
After updateRecords(collection, 1245, "tracks", "Addicted to Love"), tracks should have the string Addicted to Love as the last element.
After updateRecords(collection, 2548, "tracks", ""), tracks should not be set
{ '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' ] } }
After updateRecords(collection, 1245, "albumTitle", "Riptide"), albumTitle should be the string Riptide
// 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' ] } }
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0.

Challenge: Record Collection

Link to the challenge:

now consider using a different object for your function

like

var obj = {11: {artist: "Annalisa"}};
console.log(updateRecords(obj, 11, "title", "Mentre tutto cambia"));

what happens?

Do you mean change the name of the object that holds the record collection to obj instead of collection and add that property to it?

@ieahleen is referring to the object that gets passed to the updateRecords function. The function should work with any arbitrary object when it is passed in with an argument.

Ooooh! Ok great! Let me know if this is correct –
var obj = {11: {artist: “Annalisa”}};

console.log(updateRecords(obj, 11, “title”, “Mentre tutto cambia”));

output:
TypeError: collection[id] is undefined

So you can pass any object name through the function without referring to “collection” as the obj name?
*edit, erroneously referred to it as “id”

I apologize for a second reply, I think I get it, that if you declare another object, and then try to pass it through the function, it should work. I think I am confused because the function specifically refers to only one object

The above line calls the function and passes collection as the first argument, which is one of many possible objects that could be passed to this function. Inside the function, you reference obj because it is the same object that gets passed to the function. The parameter name is just a placeholder that acts as a local variable to the function.

1 Like

This function should not refer to specifically one object. That’s why it has an argument for the object. In @ieahleen’s example, she wants to add the title “Mentre tutto cambia” to the record 11 in her object, but your function would only look at the collection object.

You should be able to have multiple record collections that updateRecords can update, but your current solution will only update one single collection.

Totally! Right before you replied I changed “collection” to “object” (it clicked in my mind to try it after your replies) and then passed a different argument through it, (“obj”), and it worked.

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

  if (value === "") {

    delete object[id][prop];

  } else if (prop === "tracks") {

    object[id][prop] = object[id][prop] || [];

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

  } else {

    object[id][prop] = value

  }

  return object;

}

var obj = {11: {artist: "Annalisa"}};

console.log(updateRecords(obj, 11, "title", "Mentre tutto cambia"));

Output:
{ '11': { artist: 'Annalisa', title: 'Mentre tutto cambia' } }

That’s awesome. When I try to add tracks, it still gives me no empty array and no ID for “tracks” created. –

var obj = {11: {artist: "Annalisa"}};

console.log(updateRecords(obj, 11, "tracks", ""));

Output:
{ '11': { artist: 'Annalisa' } }

Also thank you, this is all very helpful.

Good job working through it!


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Thank you!!
It’s weird though, because the code is still not passing the “test”

when value is an empty string it deletes the property
try with

console.log(updateRecords(obj, 11, "tracks", "Senza riserva"));
console.log(updateRecords(obj, 11, "tracks", "Non cambiare mai"));

EDIT: the last function you have written passes for me

1 Like

Oh, you’re right! I just ran the test, and it passed. Amazing! Thank you!