You start with an updateRecords function that takes an object like collection , an id , a prop (like artist or tracks ), and a value . Complete the function using the rules below to modify the object passed to the function.
Your function must always return the entire object.
If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.
If value is an empty string, delete the given prop property from the album.
Note: A copy of the collection object is used for the tests.
Your code so far
// Setup
var collection = {
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(object, id, prop, value) {
if(prop!="track" && value=="")
{
var newProp = value;
return object[3].prop = newProp;
}
else if(prop=="track"&&id.prop==undefined)
{
var array=[];
return array = value;
}
else if (prop == "track" && track!="")
{
return track.push(value);
}
else if(id.track=="")
{
return delete id.track;
}
return object;
}
updateRecords(collection, 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/86.0.4240.111 Safari/537.36.
not completely confused. my problem is how to connect the arguments with the collection objects, tried different approach but is not working. right now i think the solution is what i need so i can move ahead. i will always revisit javascript object later.
Ok this code is very wrong. Most of JS involves using functions and manipulating objects. If you haven’t got these down, you’re likely to have severe issues going forward.
If I take one of the instructions:
If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
You’ve written:
if (prop!="track" && value=="") {
var newProp = value;
return object[3].prop = newProp;
}
So the function has some arguments: object which represents that collection, id is the id of the record (eg “5439”), prop is one of the properties of that record (eg “tracks”), value is a value to give the property.
it’s “tracks” not “track”
there is no key “3” on the object, and you have the id in the function anyway.
there is no prop called “prop”, that’s what you’re assigning to atm.
it asks you to return the whole object
you access an object like object[key]. So you have an object called object, and you have an id id. So you would access it like object[id]. That gives you the record based on the id.
this is also an object, so you access the property on it the same way: object[id][prop]
you set values of object properties like object[id][prop] = value
if (prop != "track" && value == "") {
object[id][prop] = value;
}
So if the function was updateRecords(collection, "5439", "artist", "ABBA"), then
object is collection
id is "5439"
prop is "artist"
value is "ABBA".
So object[id][prop] = value evaluates as collection["5439"]["artist"] = "ABBA"