Record Collection CHallenge Doubt

Record Collection CHallenge Doubt
0

#1

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?

switch(prop){
      case "tracks":
        console.log(prop);
        collectionCopy[id].prop.push(value);
        console.log(collectionCopy[id].prop);
        return "Agregado";
      break;
      case "artist":
        collectionCopy[id].prop = value;
        console.log(collectionCopy[id].prop);
        return collectionCopy[id].prop;
      break;
    }

#2

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.


#3

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");

#4

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.

#5

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

collection[id].prop.push(value)

but

collection[id].tracks.push(value)

works if the

console.log(prop);

tells me that prop = tracks?

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


#6

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.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables/


#7

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


#8

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…


#9

Secon case of switch


#10

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";

#11

I think that is the problem

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


#12

Take a few minutes and redo these challenges below:

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-dot-notation

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-bracket-notation

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables


#13

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


#14

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