Please i need help with this spent more than two hours on this 😭

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

// Setup
const recordCollection = {
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(records, id, prop, value) {
if (prop !== "tracks" && value !== " ") {
  records[id] [prop] = value;
} else if (prop === "tracks" && records[id].hasOwnPropert("tracks") === false) {
  records[id] [prop] = [value];
} else if (prop === "tracks" && value !== "") {
  records[id] [prop].push(value);
} else if (value === "") {
  delete records[id] [prop];
}
return records;
}
updateRecords(5439,  "ABBA"),
updateRecords(recordsCollection, 5439, "tracks", "Take a Chance on Me");
updateRecords(recordsCollection, 2548, "artist", ""),
updateRecords(recordsCollection, 1245, "tracks", "Addicted to Love"),
updateRecords(recordsCollection, 2468, "tracks", "Free"),
updateRecords(recordsCollection, 2548, "tracks", ""),
updateRecord(recordsCollection, 1245, "albumTitle", "Riptide"),
{

}
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36

Challenge: Record Collection

Link to the challenge:

In the third condition, you also need to check if the tracks property already exists in the record or not.


if you see the condition carefully, it asks to add value to the existing tracks array.

I don’t quite get you

I meant that in your third if statement you are checking if the property is tracks and the value is not an empty string, which is correct. But you also need to check if the record already has the tracks property existing or not, just like you are checking in your second if condition. Coz you need to work on the record only if the tracks property already exists.

if (prop !== "tracks" && value !== "") {
  records[id] [prop] = value;

" " with a space between the quotation marks does not equal to an empty string. Just remove the space like in the example above

else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
  records[id] [prop] = [value];
}

this was a typo Propert vs Property

1 Like

To check for empty strings always use "" no spacing in between quotations.
and I guess a typing mistake .hasOwnProperty()
Don’t worry these types of mistakes won’t happen while using code editors.

Well you can always use debugging console.log() within every if
else if blocks and see which all arguments entered which condition.
You’ll get to know when an argument is not entered in a particular block where it was supposed to. Hence check the conditions, to know what went wrong.

1 Like

Yes what @andrewdyquia1 said is also true. Remove the white space from the quotes while checking for an empty string in your first if statement here:
image

And also check if ‘tracks’ is a property of the record with that id in your third if statement just like how you checked here in your second if condition:

image

And there is also a typo for hasOwnProperty() in your second if as @andrewdyquia1 said.

So how do I go about this

What have you tried to change so far?

Please try all the changes suggested in my last reply and if it still doesn’t work, share your code once again!

I’m also stuck in this one. I don’t get this line

          records[id][prop] = [];
          records[id][prop].push(value);

Is the array called prop? is it the same as if I was writing prop: = []; in the object?

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

It is not the same. prop by itself is not an array. prop is a variable which holds a property of a record with specific id in the recordCollection.

Just splitting it all up:

So if we want to pick a record with a specific id then we refer it as recordCollection[id].
For example, recordCollection[1245] will pick this record from the collection
1245: {
artist: ‘Robert Palmer’,
tracks:
}
Now to get the value of any property within that record we need another level of indexing.
For example, if we want to get the artist name, we need to capture it as
recordCollection[1245][“artist”]
This will give “Robert Palmer”.

So recordCollection[id][prop] gives us the value of the property within the record with that id.

1 Like

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