Record Collection again

Hi all, I’m reviewing JavaScript and got to the Record Collection challenge quite well. I wanted to do it from scratch and I think I’m close and just a little explanation on details I’m missing will help me pass it without resorting to the solution.
So, if you’re inclined to do so, I thank you very much.
Here’s my 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 (prop !== "tracks" && value !== "") {
  object[id][prop] = value;
} else if (prop === "tracks" && !object[id].hasOwnProperty(value)) {
  object[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
  object[id][prop].push(value);
} else if (value === "") {
  delete object[id][prop];
}
return object;
}
updateRecords(collection, 5439, 'artist', 'ABBA');```
And here are the error messages:
// running tests 
After updateRecords(collection, 5439, "tracks", "Take a Chance on Me"),
tracks should have Take a Chance on Me as the last element.
After updateRecords(collection, 1245, "tracks", "Addicted to Love"), tracks should have Addicted to Love as the last element.
After updateRecords(collection, 2468, "tracks", "Free"),  tracks should have
1999 as the first element.
 // tests completed

May it be I didn't call the array "tracks" appropriately?

If prop is equal to tracks and object does not have prop equal to tracks not album

1 Like

@MrBondx Thank you very much I corrected that but I’m receiving the same errors

Well, on that same condition you’ve got an empty array. Is that the requirement? And on the next condition you use arr.push - it will probably throw an error ‘arr is not defined’

@MrBondx Thank you. I still have issues I see.
The empty array is a requirement. And the arr in the next condition doesn’t throw an error at all. In fact the challenge seems to be working with no syntax errors until I run the tests.
Guess I’ll have to study it better. Thanks.
P.S.: I replaced the “arr” with “object[id][prop]”. Same.

only one statement will execute in a if/else if/else chain - you are making the empty array and doing nothing with it

not only that, seeing that the condition is object[id].hasOwnProperty("tracks"), if the tracks property is present, you are overwriting it

1 Like

@ilenia Thank you for your patience ieahleen. I added value to the empty array, I guess I’ll have to think better what property I have to check.
But talking in general, I’m better off than before, don’t you think? As I said, I still have issues but not so disastrous as before.
I’ll keep on studying and studying.
Thank you.

I can’t make a comparison, I don’t remember your previous attempts

I see some logic difficulties, those will get better with practice

1 Like

@ieahleen Thank you! It’s good to know that if I try hard I’ll make it. Else if I don’t, I won’t.

“If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it”

Remember to add value to it!

Also, here:

arr.push("tracks");

You need to push value instead of “tracks”.

@Frakkoz Thank you, I had done the adding of value but didn’t add it here!
I changed it to push value instead of “tracks”.
I’m still receiving the same errors though I have no syntax errors.
So thanks, I’ll keep on trying!

I just noticed on last thing:

else if (prop === "tracks" && object[id].hasOwnProperty("tracks")) {

On this line, the second condition should return false.
One way of achieving this could be by adding a ! before it, like this:

else if (prop === "tracks" && !object[id].hasOwnProperty("tracks")) {
1 Like

@Frakkoz I was turning my head around just to figure out where I had to add a “!”! Thank you so much! I’ll check the inverted commas too, thanks to the changes you suggested I’m receiving less errors!

1 Like

Well, @Frakkoz, almost there.
// running tests
After updateRecords(collection, 2468, “tracks”, “Free”), tracks should have 1999 as the first element.
After updateRecords(collection, 2548, “tracks”, “”), tracks should not be set
// tests completed

what’s your updated code?

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

if (prop !== "tracks" && value !== "") {

  object[id][prop] = value;

} else if (prop === "tracks" && !object[id].hasOwnProperty(value)) {

  object[id][prop] = [value];

} else if (prop === "tracks" && value !== "") {

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

} else if (value === "") {

  delete object[id][prop];

}

return object;

}

I’m sorry @ilenia I had some problems at home. It’s the one in the last post.

and what do the tests say?

Here they are @ilenia

I’m pretty sure that the only thing that isn’t correct right now is this:

!object[id].hasOwnProperty(value)

On this line, you should exchange value with "tracks".

For reference: freeCodeCamp Challenge Guide: Record Collection

1 Like