Could somebody help explain this to me: NEW

I was hoping you could help me understand this:

var collection ={
  "2548":{
    "album":"Slipperey 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"
    }
  }

var collectionCopy=JSON.parse(JSON.stringify(collection))

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
  }

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

I understand everything until the function starts (like what is happening in each line of the function after it is declared?)

Well, I see that this challenge is a bit tricky.
I will try to explain it:

The first line is the declaration, the function will take three arguments, id(the id of the collection in the JSON object), “prop” is the property, it could be “artist”, “album” or “tracks”, and value. The function will take the id an the property and will update that property with the new “value”, that given in the function call.

function updateRecords(id, prop, value)

Now you will test something. First the function tests if the value is an empty string. If that is true it will delete these property from the collection that you supply. this part handles the cases were you want delete some value in property :

 if (value===""){
delete collection[id][prop]
}

If that is not true(you are supplying some value) then the function will test if the property is “tracks”:

 else if (prop===“tracks”){
collection[id][prop]=collection[id][prop] || []
collection[id][prop].push(value)
}

I explain in detail this in the follow link:

http://forum.freecodecamp.org/t/record-collection-question/382808/4?u=rigo-villalta

If the property is not “tracks” and value is not empty, the function simply updates the value in the wanted property:

 else{
 collection[id][prop]=value
 }

Finally you return all the collection updated:

return collection

I hope it helps.

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.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums