Im so confused on this problem

Tell us what’s happening:

honestly i just dont understand how this works at all

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

// Only change code below this line
function updateRecords(id, prop, value) {
if (value === "") {
  delete collection[id][prop];
} else if (prop === "tracks") {
  collection[id][prop] = collection[id][prop] || [];
  collection[id][prop].push(value);
} else {
  collection[id][prop] = value;
}

return collection;
}

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

console.log(updateRecords(5439, "artist", "ABBA"));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Challenge: Record Collection

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

It’s difficult to give any help if you don’t point out what you don’t understand. The solution you posted passes the tests, but if you don’t understand why it works then did you just copy/paste it from somewhere?

2 Likes

I’ll try to break down the answer function as best as I can.

You are passing three arguments to the function – id, prop, and value.

For the “delete” portion…
If you only pass in an empty string ("") for “value”, then whatever matches collection[id][prop] will be deleted. One of the examples in the testing of the problem is updateRecords(2548, “tracks”, “”)
In that case, you’re going to the item of the object which matches the id property 2548 – which is the Bon Jovi record – and after the function finishes, the “tracks” property will be deleted.

for the “tracks” portion…
if the prop passed in is “tracks”, this is a special case.
First, it sets the property to either itself or an empty array. This is so that if no “tracks” property exists, it will initialize the property as an empty array. It needs to do this part so that if no tracks array existed prior, it will be able to use the Array.push() method in the next line. On that line, it goes to the tracks array, and uses the push() method to add the “value” passed into the function to the end of the tracks array, as requested in the challenge description.

For all other properties…
If the passed in “prop” is not “tracks”, it will simply update (or add if necessary) the property and set its value to “value” – whatever you passed into the “value” parameter as an argument.

Finally, as requested in the directions, the function returns the newly updated collection.

Hopefully that helps clarify what’s going on for you. :+1:

1 Like

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

1 Like

okay i didnt know that thank you!

I guess im confused on how the computer knows how to correlate between the positions of the arguments id, prop, value and the positions of the record information

Very said thank you!!

it doesn’t know
you need to write yourself a function that uses the correct variables

what the code does is, it takes the values passed in the function call, example updateRecords(1234, "tracks", "Singing in the rain"), and put them in the parameters of the function, so in this case id would have value of 1234, prop would have value of "tracks" and value would have value of "Singing in the rain"
this is what the code knows
everything else depends on how you write the algorithm, using all that stuff you learned till now, like manipulating objects, if/else statements, manipulating arrays, etc

1 Like