Record Collection CHallenge Doubt

I have a doubt doing this challenge. When I use .push() in the variable prop instead .tracks I can’t update the collection but if I use the varible in the second case the program is OK. Why does this happen?

      case "tracks":
        return "Agregado";
      case "artist":
        collectionCopy[id].prop = value;
        return collectionCopy[id].prop;

You have a couple issues here:

  1. When you use dot notation the way you have above, there must be an actually property named “prop” in the object. Since there is not a “prop” property, then collectionCopy[id].prop is undefined and undefined does not have a push method.

  2. Assuming you figure out how to reference a property name via a variable (prop), you will another issue. The issue is what if there is no “tracks” property currently in collectionCopy[id]? That means there is no array to push the value to, so you would get an error.

prop is the variable used in the function an it’s referenced to a key called “tracks”. I have a if condiftion to solve if “tracks” not exists

var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 

function updateRecords(id, prop, value) {
  //My solution

updateRecords(5439, "tracks", "red");

I understand prop is a variable. You need to think about a way (you have already learned in a previous challenge) for how to detect if a property exists in an object or not. The logic would be something like below. Keep in mind, these are not necessarily the actual if statement conditions, but it is the logic your code needs to have in some way.

if property is "tracks" and the "tracks" property exists in object then push value to the "tracks" property. 

if property is "tracks" and the "tracks" property does not exist in object, then create a "tracks" property and assign it an empty array.  After that, push the value into this array.

I learned how to detect if a property exists, my doubt is why I can’t use




works if the


tells me that prop = tracks?

Maybe prop is taking “tracks” like a string not like array?

prop is a variable. When you want to reference a property name of an object with a variable, you must use bracket notation. Please review the previous challenge below for more information.

But I used prop in the second case with dot notation an it works.

Above is the second case and you did not use a property named “prop”. You used a property named “tracks” but not “prop”.

I think you are confusing a property named “prop” and the variable prop. The first reference of “prop” (which is what using prop after the dot means) is the actual string “prop”. The prop variable contains a string value, but it could be “tracks”, “artist”, “album”, etc…

Secon case of switch

As I already explained in my first reply, this line will ONLY work if there are already a “tracks” property in collectionCopy[id]. See my previous replies on what you must do if the “tracks” property does not exist in the collectionCopy[id].

Also, why are using the following in your code?

return "Agregado";

I think that is the problem

I used that to test if the condition works is like “Inserted” in spanish

Take a few minutes and redo these challenges below:

Thanks for helping me. I think a need to do more challenges with objects to understand the subject.

If you have any more questions, there is typically someone on the forum who can answer.