So I am trying to do this my way, and not completely copy the solution code. Although, I can’t seem to make this code work. What I have makes sense to me, but something is not registering right, and I can’t figure it out. Help!
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 !== 'tracks' && value !== "") {
object[id][prop] == value;
} else if (prop === 'tracks' && object[id][prop] !== 'tracks') {
object[id][prop] == [];
object[id][prop].push(value);
} else if (prop === 'tracks') {
object[id][prop].push(value);
} else if (value === "") {
delete object[id][prop];
}
console.log(collection);
return object;
}
updateRecords(collection, 5439, 'artist', 'ABBA');
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:81.0) Gecko/20100101 Firefox/81.0.
If the prop is tracks and the collection object property does not have a tracks property do this…
I updated code based off of this question
// 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 !== 'tracks' && value !== "") {
object[id][prop] == value;
} else if (prop === 'tracks' && object[id][prop].hasOwnProperty('tracks') === false) {
object[id][prop] == [];
object[id][prop].push(value);
} else if (prop === 'tracks' && value !== "") {
object[id][prop].push(value);
} else if (value === "") {
delete object[id][prop];
}
console.log(object);
return object;
}
updateRecords(collection, 5439, 'artist', 'ABBA');
// running tests
After updateRecords(collection, 5439, "artist", "ABBA"), artist should be ABBA
After updateRecords(collection, 5439, "tracks", "Take a Chance on Me"), tracks should have Take a Chance on Me as the last element.
After updateRecords(collection, 2548, "tracks", ""), tracks should not be set
After updateRecords(collection, 1245, "albumTitle", "Riptide"), albumTitle should be Riptide
// tests completed
console.log(Object.keys(collection)) if I write this, which will print to the console the keys of the collection object I get [ '1245', '2468', '2548', '5439' ]
the collection object doesn’t have the tracks property by definition