This is an exercise/lesson in the Basic Javascript section of Javascript Algorithms and Data Structures. The first solution has something confusing to me in the first else if statement. In the line above, value is not in square brackets, but it is in brackets in the else if. Is it because if “tracks” does not exist (which is an array), then it creates the array called tracks? Here is the function:
function updateRecords(records, id, prop, value) {
if (prop !== 'tracks' && value !== "") {
records[id][prop] = value;
} else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
records[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
records[id][prop].push(value);
} else if (value === "") {
delete records[id][prop];
}
return records;
}
Your explanation sounds correct to me. Is there something specific you don’t understand about the code? Because it seems like you are reading it correctly.
I don’t think I would have known to put [] around the one value. I tried to do that exercise but didn’t get far before I was stumped. So I keep going back to it until I “get” it. I also noticed comments on the pages that have the solution after clicking “Get a hint”. I’ve made notes on the exercises that were difficult for me. I’ll have to go back and read the comments as well for more insight.