And the only thing wrong with your code was that u were using global variable collection
within your function which you should not. So to use such variables which are outside of you function u pass a parameter for your variable in that function and use that parameter as that variable and at the end when you define parameters of function you give its value like this:
updateRecords(collection,5439,"artist","ABBA");
or whatever the values u want to use. By doing this u can use your function to work on any number of variables or objects and that is the use of function .
Just think this about like if u are to update Records of some big platform there will be so much numbers of objects that u cant write a function for each of it u have to make function that can work on maximum numbers of objects and update there record.
For Example i have an another object named another
var another ={
2548: {
albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: ['Let It Rock', 'You Give Love a Bad Name']
}
};
and i want to change it just as i did in previous function i will not write another function for it i will just use this line of code to do same for it:
updateRecords(another,2548,"artist","Bon Jovi");