Jenovs had a good breakdown. Similar to what we probably should have been able to figure out with the instructions from the lesson.
Like many others, this lesson stumped me. I had to sit here reading forums to truly grasp it. There seems to be many ways to simplify the code, but I wrote it in a way that made the most sense given the instructions. Looking around for other answers I came across -
function update(id, prop, value) {
if (value === ‘’) {
delete collection[id][prop];
} else if (prop !== ‘tracks’) {
collection[id][prop] = value;
} else {
collection[id][prop].push(value);
}
return collection;
}
I can’t find myself coming up with this code with the instructions given. I’m just not there yet. So following along with the information given and commenting it out and then figuring it out from there gave me an easier understanding.
// Only change code below this line
function updateRecords(id, prop, value) {
//If prop isn’t “tracks” and value isn’t empty (""), update or set the value for that record album’s property.
if (prop !== “tracks” && value !==""){
collection[id][prop]=value;
}
//If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.
- This was probably the hardest part for me to really figure out on my own.
if(prop === “tracks” && collection[id].hasOwnProperty(“tracks”) === false){
collection[id].tracks = [];
}
//If prop is “tracks” and value isn’t empty (""), push the value onto the end of the album’s existing tracks array.
if(prop === “tracks” && value !== “”){
collection[id][prop].push(value);
}
//If value is empty (""), delete the given prop property from the album.
if(value === “”){
delete collection[id][prop];
}
return collection;
}