Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. // Write ES2015 code and import modules from npm
  2. // and then press "Execute" to run your program.
  3.  
  4. /*
  5. Please write a function that checks if two objects have the same content.
  6. */
  7. /* NOTES:
  8. 1. Ignore _id.
  9. 2. Keys in the objects are unknown to this function.
  10. Please don't do:
  11. offerA.rate equals offerB.rate and
  12. offerA.offerType equals offerB.offerType
  13. ...etc
  14. 3. Write smaller functions if necessary and name them well.
  15. 4. Write comments if necessary.
  16. 5. Below are the two objects we want you to compare and the result should be true.
  17. 6. WHEN COMPLETE, ACTIONS -> SAVE PRIVATE GIST, AND INCLUDE THE LINK IN THE WRITTEN INTERVIEW.
  18. */
  19.  
  20. // Optional: Checkout Ramda. It has already been imported in this module for you.
  21. import R from 'ramda';
  22.  
  23. const offerA = {
  24. rate: "freeOfCharge",
  25. bookingCode: "FREELOCAL",
  26. allowCombination: false,
  27. offerName: "Bring a Local for FREE! Promotion",
  28. specialConditions: "Please note, this special offer can be booked today, and is valid for travel at any time during the year except for December 24th through January 6th.",
  29. offerType: "Per person",
  30. _id: "57f7019e18f4fc0300a9e6d1",
  31. discountType: "percentage",
  32. validDatesOfTravel: [
  33. {
  34. fromDate: "2017-04-01T00:00:00.000Z",
  35. toDate: "2017-12-24T00:00:00.000Z",
  36. _id: "123",
  37. updatedAt: "2016-06-28T21:20:21.754Z",
  38. createdAt: "2016-06-28T21:20:21.754Z"
  39. },
  40. {
  41. _id: "57f7019e18f4fc0300a9e6d3",
  42. fromDate: "2018-01-06T00:00:00.000Z",
  43. toDate: "2018-03-31T00:00:00.000Z",
  44. updatedAt: "2016-08-17T20:21:08.723Z",
  45. createdAt: new Date("2016-08-17T20:21:08.723Z")
  46. }
  47. ],
  48. tags: ['free', 'multi'],
  49. rates: [{nett: 2, commission: '20%'}]
  50. };
  51. const offerB = {
  52. _id: "57f7019e18f4fc0300a9e6d1",
  53. rate: "freeOfCharge",
  54. bookingCode: "FREELOCAL",
  55. allowCombination: false,
  56. offerName: "Bring a Local for FREE! Promotion",
  57. specialConditions: "Please note, this special offer can be booked today, and is valid for travel at any time during the year except for December 24th through January 6th.",
  58. offerType: "Per person",
  59. discountType: "percentage",
  60. validDatesOfTravel: [
  61. {
  62. fromDate: "2018-01-06T00:00:00.000Z",
  63. _id: "57f7019e18f4fc0300a9e6d3",
  64. toDate: "2018-03-31T00:00:00.000Z",
  65. updatedAt: new Date("2016-08-17T20:21:08.723Z"),
  66. createdAt: "2016-08-17T20:21:08.723Z"
  67. },
  68. {
  69. fromDate: "2017-04-01T00:00:00.000Z",
  70. toDate: "2017-12-24T00:00:00.000Z",
  71. updatedAt: "2016-06-28T21:20:21.754Z",
  72. createdAt: "2016-06-28T21:20:21.754Z",
  73. }
  74.  
  75. ],
  76. tags: ['multi', 'free'],
  77. rates: [{commission: '20%', nett: 2}]
  78. };
  79.  
  80. // YOUR CODE STARTS HERE.
  81.  
  82. const compareObjects = function (object1, object2) {
  83.  
  84. // creates sorted arrays out of the keys in each object
  85. let obj1keys = Object.keys(object1).sort();
  86. let obj2keys = Object.keys(object2).sort();
  87.  
  88. // if these sorted arrays arent equal the objects aren't equal
  89. if (obj1keys.toString() !== obj2keys.toString()) {
  90. return false;
  91. }
  92.  
  93. // instantiates new empty objects
  94. let sortObj1 = {};
  95. let sortObj2 = {};
  96.  
  97. // loops through the sorted array of keys and builds both objects with the right values at the right keys
  98. let length = obj1keys.length;
  99. for(let i = 0; i < length; i++) {
  100. if (obj1keys[i] !== '_id') {
  101. if(Array.isArray(object1[obj1keys[i]])) {
  102. sortObj1[obj1keys[i]] = sortArray(object1[obj1keys[i]]);
  103. sortObj2[obj2keys[i]] = sortArray(object2[obj2keys[i]]);
  104. } else {
  105. sortObj1[obj1keys[i]] = object1[obj1keys[i]];
  106. sortObj2[obj2keys[i]] = object2[obj2keys[i]];
  107. }
  108. }
  109. }
  110.  
  111. // turns the new objects into strings to check equality
  112. let obj1String = JSON.stringify(sortObj1);
  113. let obj2String = JSON.stringify(sortObj2);
  114. return obj1String == obj2String;
  115. };
  116.  
  117. const sortObject = function(obj) {
  118. let objkeys = Object.keys(obj).sort();
  119. let sortobj = [];
  120. for(let i = 0; i < objkeys.length; i++) {
  121. if(objkeys[i] !== "_id" && objkeys[i] !== "createdAt" && objkeys[i] !== "updatedAt") {
  122. sortobj.push(obj[objkeys[i]]);
  123. }
  124. }
  125. return sortobj;
  126. };
  127.  
  128. const sortArray = function(array) {
  129. let length = array.length;
  130. for (let i = 0; i < length; i++) {
  131. if (typeof array[i] === "object") {
  132. array[i] = sortObject(array[i]);
  133. }
  134. }
  135. let sortedArray = array.sort();
  136. return sortedArray;
  137. };
  138.  
  139. console.log(compareObjects(offerA, offerB));
  140.  
  141. // WHEN COMPLETE SAVE AS PRIVATE GIST AND INCLUDE THE LINK IN THE WRITTEN INTERVIEW.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement