Tell us what’s happening:
I took a look at the solution for the Record Collection challenge and I think I understand most of it. I’m super new to programming so I’m trying to get a better understanding of all this.
My first run at this had me using .hasOwnProperty to look if there was a property that existed called “tracks” which didn’t work. But then looking at the solution, I don’t understand the if/else check within the if statement. Can someone write some pseudocode to help explain it? It looks to me like it’s saying “If the property passed through in the argument exactly matches the string “tracks” and the value argument passed in isn’t a blank string, check to see if the property passed in exists, if it does - push the value argument to that property. If it doesn’t contain the property, add the property and give it the value argument.” All of that makes sense to me I think. But the else if statement is what’s tripping me up. I don’t see the difference between that one and the one in the else statement above it except that the word “value” is in square brackets in the first one. Can someone help me understand the syntax there? Thank you so much!
Your code so far
// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if (prop === "tracks" && value !== "") {
if(collection[id][prop]) {
collection[id][prop].push(value);
}
else {
collection[id][prop]=[value];
}
} else if (value !== "") {
collection[id][prop] = value;
} else {
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
.