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.
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
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');
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?
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;
}
}
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.