Stuck on Record Collection

So I’ve finally got back to the record collection lesson and I’m stuck. I do have a far more solid understanding of Javascript this time. Hopefully I’m not stuck for ten days this time. :sweat_smile:

I really can’t think of another way to do this part of the lesson other than the way I’m doing it.

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

Can anyone give me some pointers as to what I’m missing?

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

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
  **Your browser information:**

User Agent is: Mozilla/5.0 (iPad; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/102.0 Mobile/15E148 Safari/605.1.15

Challenge: Record Collection

Link to the challenge:

In this line of code:

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

what is tracks ?

The albums tracks property that is a string. I don’t know what else it could be if it isn’t that.

The albums tracks property that is a string

Yes, exactly right.

Now, do you know the difference between a variable name and a string?

1 Like

This might help - look for the definition of a string literal:

And don’t worry if you still see something like “prop is not defined”.
There’s one more error to fix, but it’s a really easy one.

1 Like

Your function finishes here, so it’s not doing anything

1 Like

Whoop! Blimey can’t believe I missed that one. :sweat_smile:

I’ve got this now, which ticks two boxes, the string mix up feels a little silly in retrospect. :sweat_smile:

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


if(prop !== "tracks" && value !== ''){
records[id][prop] = value;
}
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Well done! You’ve made a lot of progress in the past couple of weeks. :smiley:

Now you just need some else if statements for the remaining conditions.
Hopefully they’ll be a bit easier this time.

3 Likes

I’ve got this now. I think I’m sort of on the right lines with some of this. I think hasOwnProperty is the right tool for these two if else’s.
But I think my execution is off.

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


if(prop !== "tracks" && value !== ''){
  records[id][prop] = value;
}
else if (prop.hasOwnProperty('tracks')){
  records[id][prop].push([] + value);
}
else if (prop.hasOwnProperty('tracks') && value !== ''){
tracks + value;
}

return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

hasOwnProperty is a method on objects… Is prop an object? Where would the 'tracks' property exist if it was defined?

1 Like

No prop isn’t an object, whoops!

But records is an object.

Hello

I don’t think + operator is needed here.
Consider refresh memories about syntax of push method

1 Like

It isn’t, push is needed I think. Which is why I have this code now. I feel like I’m getting close here but still missing a few things. :thinking:

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

if(prop !== "tracks" && value !== ''){
records[id][prop] = value;
}
else if (records[id][prop].hasOwnProperty('tracks')){
records[id][prop].push([] + value);
}
else if (records[id][prop].hasOwnProperty('tracks') && value !== ''){
records[id][prop]['tracks'].push(value);
}
else if (value == ''){
delete records[id][prop];
}
else{
return records;
}
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

This line. Rephrasing, records[id]['tracks'] does not exist and you need to create it. How do you set the value of a property that doesn’t exist same way as you set any other property? What should it be set to an array that only holds the one value?

1 Like

I don’t know how to do either of those things. I tried to create tracks using what I learned from adding new properties to a Javascript object, but it didn’t work.

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

if(prop !== "tracks" && value !== ''){
records[id][prop] = value;
}
else if (records[id][prop].hasOwnProperty('tracks') ){
records[id][prop].push([] + records[id][prop].value);
}
else if (records[id][prop].hasOwnProperty('tracks') && value !== ''){
"tracks" + value;
}
else if (value == ''){
delete records[id][prop];
}
else{
return records;
}
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

This case was correct. Don’t go breaking good code! Here is what you had:

// Only change code below this line
function updateRecords(records, id, prop, value) {
  if (prop !== "tracks" && value !== '') {
    // looks good
    records[id][prop] = value;
  } else if (records[id][prop].hasOwnProperty('tracks')) { // this condition will need tweaking 
    // this isn't how you set a property of an object 
    records[id][prop].push([] + value);
  } else if (records[id][prop].hasOwnProperty('tracks') && value !== '') { // also small tweak needed here
    // looks mostly good - small tweak needed
    records[id][prop]['tracks'].push(value);
  } else if (value == '') {
    // looks good
    delete records[id][prop];
  } else {
    // why is this trapped in an else clause?
    return records;
  }
}
1 Like
records[id][prop] = value;

This line is good. What does this syntax do? Can you explain it in words?

1 Like

else if (records[id][prop].hasOwnProperty(‘tracks’)) { // this condition will need tweaking

What I’m stuck with here is that I know how to say in Javascript the ‘if prop is tracks part. But I don’t know how to say in Javascript the ’ but the album doesn’t have a tracks property’ part.

// this isn’t how you set a property of an object

I don’t know what I’m missing here. I’ve gone over the lesson and another resource I was linked the last time I attempted this. I know how to set a property to an object in theory but I can’t get it to work here.

// why is this trapped in an else clause?

the if/else if won’t work unless there’s an else at the end. If it isn’t return records I have no idea what else is supposed to go there.

This isn’t true. You do not need an else at the end. You must always return the records object.

Setting an object property as an array is exactly the same as setting the object property to be anything else.

2 Likes