Record Collection_2

Record Collection_2
0

#1

Tell us what’s happening:
I’m not sure about the logic here. This is only a part of the assignment, but I wanted to get some feedback before I went any further. First, can you put objects in if else statements? They do represent data, so I figured I could, but since I’m not done with the assignment I haven’t tested anything yet. I also have a combination of variables and objects; I’m not sure about that either. Also, does the statement ‘collection.id_2.prop_2[val_1];’ add val_1 to the object automatically? At the end of the function, I have to add the entire collection object, so I cannot return anything in any of the if or if else statements. Did I use the undefined key word correctly?

Your code so far


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

    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  var id_1 = id;
  var id_2 = collection[id_1];
  var prop_1 = prop;
  var prop_2 = collection.id_1[prop_1];
  var val = value;
  var val_1 = collection.id_2.prop_2[val];
  if(prop_2!=tracks && val_1!=undefined){
  collection.id_2.prop_2[val_1];
  }
  else if(prop==tracks && prop_2 == null){
    var ar = [];
// This is where I decided to stop for feedback.
  }
  }
  
  return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Chrome/68.0.3440.84 .

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection


#2

Let’s start with the 2 lines above. Why are you assigning id to another variable named id_1? If you want to assign something to id_2, just write:

var id_2 = collection[id];

I am not sure why you even have these the second variable id_2.

Again, this is an unnecessary declaration. Just use prop when you need to use prop.

There is no such property named “id_1” in the collection, hence trying to reference another property whose name is the value of the prop_1 variable will cause an error. Why? Because collection.id_1 is undefined and undefined values do not have properties.

Remember, when you reference an object property with dot notation, the object needs to have that exact property name.

Before going any further, I recommend you review all of the Basic JavaScript challenges involving working with objects. You seem to not understand the basic syntax of referencing object properties, so you will struggle with this challenge. Work through those challenges again by resetting the code and solve them without any help on the forum. Once you work through those object related challenges, come back an work on this challenge.


#3

I did that because in the lesson on ‘Basic JavaScript: Accessing Object Properties with Variables’ they went through this procedure to assign object data to variables like so:
js

var dogs = {
  Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle"
};
var myDog = "Hunter";
var myBreed = dogs[myDog];
console.log(myBreed); // "Doberman"

You’re right, however. I will go back and review.


#4

Make sure you are using 3 backticks and not 3 single quotes to format the code. I fixed your code again. Look at the instructions I gave you for how to find the backtick on your keyboard.

The example above uses bracket notation and not dot notation. It uses bracket notation, because that is the only way you can reference a property of an object if the property name is contained in a variable. myDog is a variable which has been assigned “Hunter” as a value.

If I tried to write the following, myBreed would be the value undefined.

var dogs = {
  Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle"
};
var myDog = "Hunter";
var myBreed = dogs.myDog;
console.log(myBreed); // undefined

#5

Can you put objects in an if/else statement? Yes. If you need to access an object’s property you only need to use bracket or dot notation.

Revisit the exercise and use the hints given. Also check the syntax for your if/else statements.