Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. // Setup
  2. var collection = {
  3. "2548": {
  4. "album": "Slippery When Wet",
  5. "artist": "Bon Jovi",
  6. "tracks": [
  7. "Let It Rock",
  8. "You Give Love a Bad Name"
  9. ]
  10. },
  11. "2468": {
  12. "album": "1999",
  13. "artist": "Prince",
  14. "tracks": [
  15. "1999",
  16. "Little Red Corvette"
  17. ]
  18. },
  19. "1245": {
  20. "artist": "Robert Palmer",
  21. "tracks": [ ]
  22. },
  23. "5439": {
  24. "album": "ABBA Gold"
  25. }
  26. };
  27. // Keep a copy of the collection for tests
  28. var collectionCopy = JSON.parse(JSON.stringify(collection));
  29.  
  30. // Only change code below this line
  31. function updateRecords(id, prop, value) {
  32. var album = collection[id]; // сначала находим альбом в collection по id
  33.  
  34. if (value == '') { // If value is empty (""),
  35. delete album[prop]; // delete the given prop property from the album.
  36. } else if(prop != 'tracks') { // If prop isn't "tracks"
  37. if (value != '') { // and value isn't empty (""),
  38. album[prop] = value; // update or set the value for that record album's property.
  39. }
  40. }
  41. // выше мы проверили, что prop != 'tracks', значит этот блок else - для prop == 'tracks'
  42. else { // If prop is "tracks"
  43. if(!album.hasOwnProperty(prop)) { // but the album doesn't have a "tracks" property,
  44. album.tracks = []; //create an empty array before adding the new value to the album's corresponding property.
  45. }
  46. if (value != '') {
  47. album.tracks.push(value); // push the value onto the end of the album's existing tracks array.
  48. }
  49. return collection;
  50. }
  51.  
  52. return collection;
  53. }
  54.  
  55. // Alter values below to test your code
  56. updateRecords(5439, "artist", "ABBA");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement