hi, I understand lessions of javaSctipt curriculum so far until this, this confused me: " to modify the object passed to the function." i googled how object can be passed to a function
and figured out that objects are passed to a function by reference… and reference in this case is “album title” or “artist” …and there is function call down like this (updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’));
so… when i console.log(2548) it displays album, artist and tracks…that is good
assigment is to Complete the function using the rules below to modify the object… if you modify the object it shoud have all propertyes isn’t it? …passed to the function., rules are:
- Your function must always return the entire record collection object.
- If
prop
isn’ttracks
andvalue
isn’t an empty string, update or set that album’sprop
tovalue
. - If
prop
istracks
but the album doesn’t have atracks
property, create an empty array and addvalue
to it. - If
prop
istracks
andvalue
isn’t an empty string, addvalue
to the end of the album’s existingtracks
array. - If
value
is an empty string, delete the givenprop
property from the album.
I solved the lesson with hint solution, but still dont understand what is purpose of that function if it adds a value and return records, why can’t I console log records and value? in function updateRecords there is 4 parameters: records, id, prop, and value… id are numbers or names of objects… prop are artist and tracks.
but what are value and records…
sorry because of that I can’t explain problem better, it would be great if someone could explain me in simple words or give me specific material,literature where I can figure that out,
**Your code so far**
// Setup
const recordCollection = {
2548: {
albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
albumTitle: '1999',
artist: 'Prince',
tracks: ['1999', 'Little Red Corvette']
},
1245: {
artist: 'Robert Palmer',
tracks: []
},
5439: {
albumTitle: 'ABBA Gold'
}
};
// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop !== "tracks" && value !== ""){
records[id][prop] = value;
}else if (prop == "tracks" && records[id].hasOwnProperty("tracks")=== false){
records[id][prop] = [value];
}else if (prop === "tracks" && value !== ""){
records[id][prop].push(value);
}else if (value == ""){
delete records[id][prop];
}
return records;
}
(updateRecords(recordCollection, 5439, 'artist', 'ABBA'));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36
Challenge: Record Collection
Link to the challenge: