FCC 283 Record Collection

In this example below you will see how to do a FCC 283 Record Collection with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by f1e2l3i4x5, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright f1e2l3i4x5 ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>FCC 283 Record Collection</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/f1e2l3i4x5/fcc-283-record-collection-JrWjXP */
// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  
  if (value == "") {
    delete collectionCopy[id][prop];  
  }
  
  if (collectionCopy[id].hasOwnProperty(prop) && prop !== 'tracks') {
    collectionCopy[id][prop]=value;
  }
  
  if (collectionCopy[id].hasOwnProperty(prop) && prop == 'tracks') {
    collectionCopy[id][prop].push(value);
  }
    
  if (!collectionCopy[id].hasOwnProperty(prop) && prop == 'tracks' && value !== '') {
    collectionCopy[id][prop]=[];
    collectionCopy[id][prop].push(value);
  }
  
   if (!collectionCopy[id].hasOwnProperty(prop) && prop !== 'tracks' && value !== '') {
    collectionCopy[id][prop]=value;
  }
  
  return collectionCopy[id];
}

// Alter values below to test your code
console.log(updateRecords(5439, "artist", "ABBA"));

Comments