Why do we need to use the delete operator in the Record Collection challenge?

Why do we need to use the delete operator in the Record Collection challenge?
0

#1

I just solved the record collection challenge after many failed attempts. I feel I still need to revisit the solution to really understand what was going on, and especially wrap my head around the delete operator.

To get everything green I had to used the delete operator in the very last ELSE statement to delete any submissions what had empty values.

In every other condition where I have valid data in the function call I will consequently make some change to the collection object. But when have a function call with incomplete data such as an empty track.

 updateRecords(2548, "tracks", "")

I think that I just I need to leave the collection object alone. and exit the function with.

return collection;

and be done with it. I can just skip the object manipulation process entirely. But in order to pass the exercise I needed to to delete the empty property like this.

delete collection[id][prop];

But since I don’t add any property in the function when the value is empty , why would I then need to delete a property that, to my knowledge, I never added in the first place? I assume the property and value only exist in the function call, but never touch the object unless I code it so. It this a matter of how the test itself is designed, or is there a good reason for developers to use delete in this manner in similar if else statements that manipulate an object?


#2

Take this test case:

After updateRecords(2548, "artist", ""), artist should not be set

Before this call, collection number 2548 has an artist property. From an interface point of view, it’s like a cryptic way of telling the program to delete that record’s artist property. So if this was a “real” program, and you wanted to delete a record’s field for some reason, this is how you’d do it.

Hope that made sense.


#3

Hello kevcomedia and thanks for replying to my post.

Your answer is very surprising to me but it does make sense.

I will have to test the function for myself outside of the code editor in freeCodeCamp
and see how a function call with empty value really affects the object.