Record Collection - no idea where to start

I’m really struggling to know where to even begin with this one.

I know I have to return all the data about a given record but I don’t know how to do this. I’ve checked the forums but the code I’ve seen there doesn’t make sense to me. I know it should at this point but I just can’t get my head around it.

Can someone please help and explain where I need to start with this? I’ve got some code but I am so confused where to go from 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 (records.hasOwnProperty(id,prop,value)){
return records;
 }
else {
  return "";
}
}
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
   **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0

Challenge: Record Collection

Link to the challenge:

HI @EllaGriff !

I would reset the lesson.

My advice would be to go through each of the 4 requirements slowly.

Start with this first one here

  • If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value.

Your first step is to figure out how to write this first if condition here
If prop isn’t tracks and value isn’t an empty string

How do you say something does not equal something in JavaScript.
Which operator would you use here?

Once you figure out that condition, then you need to figure out what goes inside the if statement.

if(condition goes here){
// some code goes here
}

As for what goes inside the if statement, that would be this part of the directions.
update or set that album’s prop to value.

Take some time to translate that into code and show us what you have come up with.

Once you have passed the first condition, then you can move onto the second.

Hope that helps!

1 Like

I’ve got this but it doesn’t work.

// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop != tracks && value != "") {prop = value;
}
return records;

}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

You first need to fix this part here

There isn’t a variable called tracks so that is why you are getting a syntax error.
But if there is a string then that error will go away.

As for the inside of the if statement, you will need to first access the individual object, then the prop using bracket notation.

You need to change this part here

So the = value part is correct but how you are accessing the prop is not correct.

If you can get that part, then it will be reused over and over again through the rest of the challenge.

Hope that helps!

1 Like

I feel embarrassed to ask this but is the object the individual albums or the whole music collection?

Both are objects. That’s what is tricky here. You have a record collection object that holds many record objects. (This is one of the ‘big step forward’ challenges)

3 Likes

It might help to see what the parameters are doing by using some console.logs

Run this code here to see what records returns

// Only change code below this line
function updateRecords(records, id, prop, value) {
  console.log(records)
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Then run this code to see what one of the objects returns.
Hint: I am using bracket notation which is helpful to you. :slight_smile:

// Only change code below this line
function updateRecords(records, id, prop, value) {
  console.log(records[2548])
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
1 Like

Hi I’ve done this but I don’t know how to use it.

It returns all the records or just one of them. But I don’t know what that means for what I should be doing with my code.

Is there something meaningful about it not returning the Id in the second bit of code? And it returning the records repeatedly in the first?

I provided you with the console.logs so you can understand how this nested object works.

A big portion of this challenge is understanding how to access properties of this nested object.

Once you understand how the nested object works then you will understand how to use the parameters correctly.

For example, how would you access the artist value inside this nested object here

const recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
}

If you understand how to do that, then you will have solved a big portion of this challenge.

1 Like

Like this I think?
recordCollection[0].artist;

Why the 0? This is where the id matters.

Because I thought that was how I’d access the first album based on previous lessons.

So is it like this?

recordCollection.2548.artist;

Also how do I know which prop I’m assigning to value to access the right prop? Or do I assign them all to value?

That’s exactly how you need to access it. The record collection is an object, not an array.

Your function argument prop is going to be a string. That string is the name of the property you want to update.

I know this isn’t right, but I really don’t know how else to do this.

// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop != "tracks" && value != "") {
  recordCollection.id.tracks = value;
}
return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Wait ignore that, I got it!
Now I’ve just got to do all the other code requirements. :sweat_smile:


// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop != "tracks" && value != "") {
  recordCollection[id].tracks = value;
}
return records;
}

Edit: put under spoilers. Sorry was just excited to have worked it out.

Careful…

You said that the property is not tracks…

Is this better? I think I got mixed up with that last one.

// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop != "tracks" && value != "") {
  recordCollection[id].prop = value;
}
else{
return records;
}
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Close. Why did you need bracket notation for id again? you need it for prop too

This is a big step forward. The rest is ironing out the other cases.

Because if I don’t use a bracket notation for id I get an error. I’ve looked at the lessons again and I’m still confused about when I should use it VS dot notation.

Is this right?

// Only change code below this line

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

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

recordCollection[id][prop] = value;

}

else{

return records;

}

}