Record Collection - no idea where to start

Dot notation is only when you have the exact, literal property name. Prop isn’t the name, it’s a variable holding the name. Therefore, you use bracket notation.

1 Like

Right, I have this code now. I need to do these two things.

‘If the prop parameter is equal to "tracks" and the value isn’t an empty string, push the value onto the end of the tracks array.’

‘if prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.’

I’ve tried but I can’t work out how to do these things at all.

// Only change code below this line

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

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

recordCollection[id][prop] = value;

}

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

tracks.push([] + value);

}

else{

return records;

}

}

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

You want to assign an array to the property tracks if this property doesn’t exist, in other words, to create the property with the name tracks and the value is empty array.
So if your current album object looks like this:

5439: {
   albumTitle: 'ABBA Gold'
 }

you want to make it looks like this:

5439: {
   albumTitle: 'ABBA Gold',
   tracks: []
 },

to be able to add value to the array or tracks.

Hi Ella

I think it would make more sense to try this one first.

‘if prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.’

(Think about what happens if the album doesn’t have a tracks property - would it really make sense to just check for an empty input value?)

1 Like

Hi, I’ve got this now. Kinda stuck what to put before .push to get things to work. And it seems to have made other things in my code break that did work.

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

[id].push("tracks :" + []);
}
else{
return records;
}
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

I’m not sure. I wasn’t sure how else to check that the album doesn’t have a tracks property.

I don’t know what happens if the album doesn’t have a tracks property. Can you explain what you mean? I know it means the album doesn’t have a tracks property, but I don’t know what that means in terms of code if that makes sense.

Dot notation with prop cannot work here.

Whoops! I copied the wrong version of my code. :sweat_smile:

That makes perfect sense :slight_smile:

It looks as though you were planninng to try this condition next:

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

If that condition evaluates to true, then the instructions ask you to

add value to the end of the album’s existing tracks array

But your code hasn’t yet checked whether the album has a tracks array.

Can you see the potential problem there?

That’s why I suggested that you tackle this condition next:

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

1 Like

Also here’s my code with correct notation for the first if. Mixed up which of my code I was using for this challenge. :sweat_smile:

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

[id].push("tracks :" + []);
}
else{
return records;
}
}

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

I can but that was what I was trying to do with that code. That’s what the && value != "" bit was for.

So I’m confused because I was trying to do that already. I’m not sure how else I could check that.

1 Like
2 Likes

This is another way, Ella. You should come back to this

1 Like

Well, the condition I pointed you to does handle the “album doesn’t have a tracks property” situation.

Don’t worry about it though - I should really let you code this the way you want to code it.

I noticed that you’re now replying to three people on this thread, which is a lot of “voices” if everyone responds.

So I’m just going to make a couple of comments on the code you posted, and then I’m going to step out for a while.
I’ll check back a little later though to see how you’re getting on and, obviously, I’ll respond if you reply to my post.

Look back at how you accessed the album in your first if statement.
You need to get hold of the “tracks” property.
Then you can push value into that array.

Just take some time to think it through.

I’ll check back later to see how you’re doing.

1 Like

Oh. Yeah, I should have thought of that. :sweat_smile:

Thanks for the link!

3 posts were split to a new topic: Record Collection help kuba

I’ve got this now. But I can’t work out how to say ‘if tracks hasOwnProperty is false, then do this’. I did look at the other lesson but I don’t remember how to solve it or even understand it now.

I think I’m going to redo a lot of earlier lessons because I don’t feel like enough about JavaScript has stuck in my head to solve this.

// Only change code below this line

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

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

recordCollection[id][prop] = value;

}

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

tracks.push([] + value);

}

else{

return records;

}

}

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

id is a number. It can’t have a property. I think you want the record object associated with this id. How do you get that? you did it in the case above this one


You shouldn’t reference the global variable at all. You should only use the function arguments.

Okay now I have this. The one tick I had for solving at least one lesson condition is gone so I guess I messed the code up more.

Still don’t know how to say 'if “tracks.hasOwnProperty is false do this” which I think has to be important. I’m going though some of the earlier lessons to see if I work anything else I’m missing, but so far I still don’t know how to solve this.

// Only change code below this line

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

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

records[id][prop] = value;

}

else if (prop == "tracks" && "tracks".hasOwnProperty){

tracks.push([] + value);

}

else{

return records;

}

}

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

tracks" is a string. It won’t have a property either.

You have to use the syntax some Object.hasOwnProperty(somePropertyName)

What is this? In words, what data does this represent?