SHARE
TWEET

Untitled

a guest Nov 23rd, 2016 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Setup
  3. var collection = {
  4.     "2548": {
  5.       "album": "Slippery When Wet",
  6.       "artist": "Bon Jovi",
  7.       "tracks": [
  8.         "Let It Rock",
  9.         "You Give Love a Bad Name"
  10.       ]
  11.     },
  12.     "2468": {
  13.       "album": "1999",
  14.       "artist": "Prince",
  15.       "tracks": [
  16.         "1999",
  17.         "Little Red Corvette"
  18.       ]
  19.     },
  20.     "1245": {
  21.       "artist": "Robert Palmer",
  22.       "tracks": [ ]
  23.     },
  24.     "5439": {
  25.       "album": "ABBA Gold"
  26.     }
  27. };
  28. // Keep a copy of the collection for tests
  29. var collectionCopy = JSON.parse(JSON.stringify(collection));
  30.  
  31. // Only change code below this line
  32. function updateRecords(id, prop, value) {
  33.   //myObj.hasOwnProperty(checkProp)
  34.   if(prop=="artist"&&value!==""){
  35.     collection[id].artist = value;
  36.   } else if (prop==="artist"&&value===""){
  37.     delete collection[id].artist;
  38.   } else if (prop==="tracks"&&value!==""){
  39.     delete collection[id].artist;
  40.   } else if (prop==="tracks"&&value===""){
  41.     delete collection[id].tracks;
  42.   } else if (prop==="tracks"&&value!==""){
  43.     collection[id].tracks.push(value);
  44.   } else if (prop==="tracks"&&value!==""&&collection[id].hasOwnProperty(prop)){
  45.     //this doesnt work
  46.     collection[id].tracks.push(value);
  47.   } else if (prop==="tracks"&&value!==""&&collection[id].hasOwnProperty(prop)===false){
  48.     // this doesnt work
  49.     var tracks = [value];
  50.     collection[id] = tracks;
  51.   }
  52.  
  53.   return collection;
  54. }
  55.  
  56. // Alter values below to test your code
  57. updateRecords(5439, "tracks", "Take a Chance on Me");
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top