JSFiddle - React, Tailwind, and code Playground

FCC challenge

by trentHarlem

JavaScript

// 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 (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 (prop === 'tracks' && !object[id].hasOwnProperty('tracks')) {
  object[id][prop] = [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 (prop === 'tracks' && value != '') {
  object[id].tracks.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;
}
console.log(updateRecords(collection, 1245, "tracks", "Addicted to Love"));
console.log(updateRecords(collection, 1245, "albumTitle", "Riptide"));
console.log(updateRecords(collection, 2468, "tracks", "Free"));
console.log(updateRecords(collection, 2548, "tracks", ""));
console.log(updateRecords(collection, 2548, "artist", ""));
console.log(updateRecords(collection, 5439, 'artist', 'ABBA'));
console.log(updateRecords(collection, 5439, "tracks", "Take a Chance on Me"));