Yes, another "Record Collection" post

Tell us what’s happening: Greetings, colleagues.
Day 3 of trying to solve this one and I am close to losing hope, haha!
Please, any guidance is welcomed and appreciated. I know I am close, but I just really don’t know what else to do.
On a somewhat unrelated note, is it ok to have two “then” (curly brackets) statements in one “if” statement? Also, how would one interpret “but” in JavaScript? I winged it with “&&”.

Thanks again.

  **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) {
if (prop != "tracks" && value != "") 
  {collection[id][prop] = value;}

if (prop == "tracks" && !collection[id][prop]) 
  {collection[id][prop] = [];
    collection[id][prop].push(value);}

if (prop == "tracks" && value != "")
  {collection[id][prop].push(value);}

else if (value == "") 
  {delete collection[id][prop];}

return collection;
}

updateRecords(collection, 5439, 'artist', 'ABBA');
  **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:

hint: there is one function parameter you never use

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

I modified your formatting to help others understand your code.

1 Like

You mean “object”?
I just don’t know how it fits into the whole thing.
Do I include it when trying to access an item in an array?

well, the first argument is the object

you made a function that works only on collection, not on any other object

the tests are made on a different object

2 Likes

To be fair, I think we could come up with a better name for that variable. Really, I’d call it recordArray or something like that. It is not actually an object but rather an array of objects.

Thank you so much ieahleen!
The problem is solved!

I’m pretty sure that’s an object

var collection = { 2548: { albumTitle: 'Slippery When Wet', artist: 'Bon Jovi', […]

awesome, good job!

Happy coding!

1 Like

Oh right, it is. For some reason my brain though it was an array. (I need to get my coffee). Its still a terrible name though.

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