After completing the challenge on Record Collection, I have come up with a functional solution.
I have a question about when to use quotes (""
or ''
) inside the propName
for hasOwnProperty(propName)
. Can you explain when to use hasOwnProperty(propName)
versus hasOwnProperty("propName")
?
Below is my solution that works:
function updateRecords(id, prop, value) {
if (prop != "tracks" && value != "") {
collection[id][prop] = value;
}
else if (prop === "tracks" && collection[id].hasOwnProperty("tracks") === false) {
collection[id].tracks = [];
collection[id].tracks.push(value);
}
else if (prop ==="tracks" && value != ""){
collection[id].tracks.push(value);
}
else if (value === "") {
delete collection[id][prop];
}
return collection;
}
Interestingly, changing hasOwnProperty("tracks")
to hasOwnProperty(tracks)
breaks the code. This is perplexing as the example I saw for hasOwnProperty(propName)
did not include quotes and failed when quotes were added around propName
.
I first learned about hasOwnProperty()
from: Testing object for Properties.