Tell us what’s happening:
Passing the
After updateRecords(collection, 5439, "artist", "ABBA")
, artist
should be ABBA
After updateRecords(collection, 2548, "artist", "")
, artist
should not be set
After updateRecords(collection, 2548, "tracks", "")
, tracks
should not be set
After updateRecords(collection, 1245, "albumTitle", "Riptide")
, albumTitle
should be Riptide
these dosen’t pass
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, 1245, "tracks", "Addicted to Love")
, tracks
should have Addicted to Love
as the last element.
After updateRecords(collection, 2468, "tracks", "Free")
, tracks
should have 1999
as the first element.
I guess it can’t be the object[id][prop].push(value) ; / object[id][prop].push([value])
because in either case they should be placed last /first. Based on if there is an array already. So I guess it must be either of the if statements.
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 isn't tracks and value isn't an empty string, update or set that album's prop to value.
if (object[id][prop] !== "tracks" && value !== "") {
object[id][prop] = value;
}
// If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it.
else if (object[id][prop] === "tracks" && !object[id].hasOwnProperty()){
object[id][prop].tracks.push([value]);
}
// If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array.
else if (object[id][prop] === "tracks" && value !== ""){
object[id][prop].push(value);
}
// If value is an empty string, delete the given prop property from the album.
else if ( value === "") {
delete object[id][prop];
}
return object;
}
updateRecords(collection, 5439, 'artist', 'ABBA');
Challenge: Record Collection
Link to the challenge: