Record Collection need an approach

Record Collection need an approach
0

Tell us what’s happening:
please help me with an approach to solution…i tried many ways.

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) {
if(collection[id]){

  if(collection[id][prop]){
     collection[id][prop]=value;
  }
    
else{
     
     collection[id][prop]=prop;


  }

}
return collection;

}



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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

Try to formulate an algorithm that a human can do.
Write itin pseudocode and see if it makes sense, then transform it to code.

For eg. You will need to search all the records, what does that suggest is needed? Well, a loop of some kind…

i spent (wasted ) a day on this… loosing hope

Hi omkarkothavale88,

If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.

If prop is “tracks” and value isn’t empty (""), push the value onto the end of the album’s existing tracks array.

If value is empty (""), delete the given prop property from the album.

Let’s disect the guide.

  1. If prop is “tracks” and value isn’t empty ("")

(First) conditional prop === “tracks” AND value != “”

  1. push the value onto the end of the album’s existing tracks array

If TRUE, create second conditional, does the property exist? If TRUE, push to array.

If FALSE (on the second conditional), meaning props === “tracks” the no existing value. put value (not push).

  1. If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.

meaning the result is FALSE, (it doesn’t have “tracks” property), create the property then add the value.

  1. If value is empty (""), delete the given prop property from the album.

Create conditional, if value != “” AND property exist, delete the property.

More help on this: Hint and Spoilers

Ok we arehere to support you. Please write your algorithm for us in english and show your full code. In the code you pasted above there was no loops…

You don’t really need nested if statements, mate. You’re supposed to be trying to fill different data in, so if and else if chain will suffice. And follow the instructions really carefully. Start with the
1) If prop isn't "tracks" and value isn't empty (""), update or set the value for that record album's property. It is the first condition you have to check and from then on build a chain of statements. The rest is very straightforward. Follow the hints!

finally i found a solution.Please review code and optimise it with best practices. Please revert.

// 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) {
if(collection[id]){
if(prop ==“artist” && collection[id][prop] != “artist” && value){
collection[id][prop] = “artist”;
collection[id][“artist”] = value;
}
else if(prop ==“tracks” && “tracks” in collection[id] && value){
collection[id][prop].push(value);
}

else if(prop ==“tracks” && !(“tracks” in collection[id]) && value){

collection[id][“tracks”] = [];
collection[id][“tracks”].push(value);

}

else if(prop ==“artist” && value ==""){

delete collection[id][prop];

}

else if(prop ==“tracks” && value==""){

delete collection[id][prop];

}

else if(prop ==“album” && value){

collection[id][“album”] = value ;

}

}
return collection;

}

// Alter values below to test your code
updateRecords(1245, “tracks”, “Addicted to Love”).tracks;

Why in the video are using “collection[id][prop]” what the bracket after bracket without space or dot means? has never shown before on any of the exercises, why use something in a course without explain it?

In “delete collection[id][prop]” what “[id][prop]” means, is the first time I see that in Javascript.

I am sure you have already met it… there are lessons about accessing object properties with variables and accessing nested objects

Yes, both like this, not like in the video:

ourStorage.cabinet[“top drawer”].folder2;
ourStorage.desk.drawer;

Hey, but thanks for your “really helpful” answer, you shouldn’t bother.

collection[id][prop];

So, if you look at collection, it is an object. It has a number of properties, but each of those top-level properties is an album ID:

collection = {
  2548: { /* a nested object */ },
  2468: { /* another nested object */ },
  1245: { /* Another sub-object */ },
  /* and so on */
};

Now, each property of that object can be accessed by two mechanisms: dot notation or bracket notation. Here’s the skinny:

  • dot notation: this would be used where you are hard-coding the property name. So for example, if I wanted to get collection.1245, I could simply hard-code that in. Boom done, works great.

  • bracket notation: This would let me do the same thing, but there’s a little more flexibility to the brackets. Inside there, I use a string (or a number, in this case) to access the property name: collection[1245] would get to the specific record, or collection[1245]["artist"] would get to the artist property of that particular record.

The advantage of bracket notation is, since I am using a value (either a string or a number) in the brackets, I can use a variable in there. By using a variable, I can define which property I want to use at runtime. So if id==5439 and prop=="artist", I can simply call collection[id] to get that particular record, or collection[id][prop], which internally would look like `collection[5439][“artist”] and get me that particular property.

Hope that helped a little!

2 Likes

Be careful of attitude, it won’t help. You have encountered both of these before, both dot notation and brackets, this is simply an extension of that. You had said you hadn’t encountered them, but as @ieahleen pointed out, you have.

You may not have seen them used in this way, but that is the point of the evolution of the exercises.

Thanks, that really helps, didn’t know that you can use brackets without space or dot like that, I just wonder why it wasn’t mentioned previously on any of the exercises.

1 Like

Much of the learning here requires a little further research. Personally, when going through all these exercises, I keep a browser tab open to https://devdocs.io/, a great resource moderated by those fine folks over at FCC (and you know who you are!) - it contains great documentation on many many languages, including javascript, html and css.

Looking up Objects in the javascript documentation is a very very revealing read. There’s a lot there. But the point here is, yes - brackets and dots can be used interchangeably. For the purpose of this exercise, though, the brackets are the only way, as you’re using a variable to get to the property name.

So, if anyone have a question a good answer would be “you should know that”?

First I thought the double bracket notation meant something different, so I checked the exercises again and found no clue, also is not mentioned on most of the web docs.

1 Like

If you have a question, and it’s something you’ve seen before, then saying “this is something you’ve encountered before, just in a slightly different way” is perfectly reasonable. Is it the best answer? Depends on the learner.

There is no “double bracket notation” going on. There is no such thing. I could as easily have done

const thisRec = collection[id];  
// remember, id = 1245, so we get that particular object - but its still an object.
//   we can get its properties with dots or brackets, same same same.

const thisPropertyValue = thisRec[prop]; 
// and here, we can use that object, and get the particular property

The lesson includes a collection of nested properties. Depending on the depth of those properties, you may have two, seven, or thirty-'leven brackets. But each level of bracket simply indicates another nested object or array.

Not going to argue the fitness of another’s answers, but just remember to treat others with the respect you’d like your own posts to be treated.

if you don’t like the answer, ask again
different people need different answers, but if you don’t ask again if the first answer is not the right one for you you will never get the right answer for you

I appreciate your time explaining this, and the link you shared.

1 Like