Advertisement
AviEzerzer

Untitled

Aug 11th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Only change code below this line
  2. function updateRecords(id, prop, value) {
  3.   //check that id exists in collection
  4.   if (collection[id] === undefined) {
  5.     return console.log(`id: ${id} is not in collection`);
  6.   }
  7.  
  8.   //set prop if it does not exists
  9.   if (collection[id][prop] === undefined) {
  10.     if (prop === "tracks") {
  11.       if (collection[id][prop] === undefined) {
  12.         collection[id][prop] = [];
  13.       } else {
  14.         collection[id][prop] = null;
  15.       }
  16.     }
  17.   }
  18.  
  19.   //assign value to non tracks key
  20.   if (prop != "tracks" && value != "") {
  21.     collection[id][prop] = value;
  22.   }
  23.  
  24.   if (prop === "tracks" && value != "") {
  25.     collection[id][prop].push(value);
  26.   }
  27.  
  28.   if (value === "") {
  29.     delete collection[id][prop];
  30.   }
  31.  
  32.   /*** */
  33.  
  34.   return collection;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement