Please I need here: Record Collection

Tell us what’s happening:

The codes are correct if you run it, please I really more explanation it this topic please me out.

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 (prop === "tracks" && value !== ""){
    if(collection [id] [prop]){
      collection [id][prop].push(value);
    } else{
      collection[id] [prop] =[value];
    } 
    
    }else if(value !== ""){
      collection [id] [prop] = value;
    } else{
      delete collection [id] [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.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

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

Let me try to explain… You have function that takes 3 paramaters updateRecords(id, prop, value).
There on first if statement if (prop === "tracks" && value !== "") you’re checking if value of PROP paramater is “tracks” string and (&&) VALUE paramater is not empty string, when you call function with certian arguments.
For example on updateRecords(5439, "artist", "ABBA"); function call prop === "artist, so not “tracks”… and if statement is falsy so it goes to else if statement and checks if value is not empty which it is not in this case: updateRecords(5439, "artist", "ABBA") , value = ABBA.
So it proceeds and do collection [id] [prop] = value; which is: adding value(which is “ABBA” in this case) to collection object. So, collection is object, you target objects properties with brackets aka [], so in this case you’re targeting object with property of 5439,
updateRecords(id, prop, value) = updateRecords(5439, "artist", "ABBA");, there it is id is 5439 in this function call. So it gets
“5439”: {
“album”: “ABBA Gold”
}

Thats collection [id] part, now to [prop]… prop is “artist”… again :
(id, prop, value) = updateRecords(5439, "artist", "ABBA"); , prop = “artist” , thats collection [id] [prop] part.

And then it just add passed value which is “ABBA” in this function call… you can look at that as
collection [id] [prop] = value; as collection [5439] [artist] = "ABBA"; But the way you wrote it with collection [id] [prop] = value; makes it dynamic, so it can work with any arguments you pass into that function call.

Hopefuly i haven’t made it even more confusing, few months ago i was struggling with same exercise so i just wanted to give a try explaning it and yep… (doesn’t even know what he was saying face)

1 Like

I’m not sure what your question is…

The code passes the tests.
Did you write this solution?
Are you having trouble understanding the solution?

1 Like

Thank you, you have done a lot. your explanation and example are pretty good, its really help me understand the challenge and I think it would help other that are struggling with the same in the future. you really sound like a pro, breaking down this challenge to my understanding is taking me from the bottom of a hill to the top :relaxed::relaxed::relaxed: thanks for your time, am very grateful.

yea, I was having trouble uderstanding the solution. but @Archaeologist03 has help me out. and thank you also.

Oh, no problems… I’m glad you found that helpful.

1 Like