Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write ES2015 code and import modules from npm
- // and then press "Execute" to run your program.
- /*
- Please write a function that checks if two objects have the same content.
- */
- /* NOTES:
- 1. Ignore _id.
- 2. Keys in the objects are unknown to this function.
- Please don't do:
- offerA.rate equals offerB.rate and
- offerA.offerType equals offerB.offerType
- ...etc
- 3. Write smaller functions if necessary and name them well.
- 4. Write comments if necessary.
- 5. Below are the two objects we want you to compare and the result should be true.
- 6. WHEN COMPLETE, ACTIONS -> SAVE PRIVATE GIST, AND INCLUDE THE LINK IN THE WRITTEN INTERVIEW.
- */
- // Optional: Checkout Ramda. It has already been imported in this module for you.
- import R from 'ramda';
- const offerA = {
- rate: "freeOfCharge",
- bookingCode: "FREELOCAL",
- allowCombination: false,
- offerName: "Bring a Local for FREE! Promotion",
- 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.",
- offerType: "Per person",
- _id: "57f7019e18f4fc0300a9e6d1",
- discountType: "percentage",
- validDatesOfTravel: [
- {
- fromDate: "2017-04-01T00:00:00.000Z",
- toDate: "2017-12-24T00:00:00.000Z",
- _id: "123",
- updatedAt: "2016-06-28T21:20:21.754Z",
- createdAt: "2016-06-28T21:20:21.754Z"
- },
- {
- _id: "57f7019e18f4fc0300a9e6d3",
- fromDate: "2018-01-06T00:00:00.000Z",
- toDate: "2018-03-31T00:00:00.000Z",
- updatedAt: "2016-08-17T20:21:08.723Z",
- createdAt: new Date("2016-08-17T20:21:08.723Z")
- }
- ],
- tags: ['free', 'multi'],
- rates: [{nett: 2, commission: '20%'}]
- };
- const offerB = {
- _id: "57f7019e18f4fc0300a9e6d1",
- rate: "freeOfCharge",
- bookingCode: "FREELOCAL",
- allowCombination: false,
- offerName: "Bring a Local for FREE! Promotion",
- 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.",
- offerType: "Per person",
- discountType: "percentage",
- validDatesOfTravel: [
- {
- fromDate: "2018-01-06T00:00:00.000Z",
- _id: "57f7019e18f4fc0300a9e6d3",
- toDate: "2018-03-31T00:00:00.000Z",
- updatedAt: new Date("2016-08-17T20:21:08.723Z"),
- createdAt: "2016-08-17T20:21:08.723Z"
- },
- {
- fromDate: "2017-04-01T00:00:00.000Z",
- toDate: "2017-12-24T00:00:00.000Z",
- updatedAt: "2016-06-28T21:20:21.754Z",
- createdAt: "2016-06-28T21:20:21.754Z",
- }
- ],
- tags: ['multi', 'free'],
- rates: [{commission: '20%', nett: 2}]
- };
- // YOUR CODE STARTS HERE.
- const compareObjects = function (object1, object2) {
- // creates sorted arrays out of the keys in each object
- let obj1keys = Object.keys(object1).sort();
- let obj2keys = Object.keys(object2).sort();
- // if these sorted arrays arent equal the objects aren't equal
- if (obj1keys.toString() !== obj2keys.toString()) {
- return false;
- }
- // instantiates new empty objects
- let sortObj1 = {};
- let sortObj2 = {};
- // loops through the sorted array of keys and builds both objects with the right values at the right keys
- let length = obj1keys.length;
- for(let i = 0; i < length; i++) {
- if (obj1keys[i] !== '_id') {
- if(Array.isArray(object1[obj1keys[i]])) {
- sortObj1[obj1keys[i]] = sortArray(object1[obj1keys[i]]);
- sortObj2[obj2keys[i]] = sortArray(object2[obj2keys[i]]);
- } else {
- sortObj1[obj1keys[i]] = object1[obj1keys[i]];
- sortObj2[obj2keys[i]] = object2[obj2keys[i]];
- }
- }
- }
- // turns the new objects into strings to check equality
- let obj1String = JSON.stringify(sortObj1);
- let obj2String = JSON.stringify(sortObj2);
- return obj1String == obj2String;
- };
- const sortObject = function(obj) {
- let objkeys = Object.keys(obj).sort();
- let sortobj = [];
- for(let i = 0; i < objkeys.length; i++) {
- if(objkeys[i] !== "_id" && objkeys[i] !== "createdAt" && objkeys[i] !== "updatedAt") {
- sortobj.push(obj[objkeys[i]]);
- }
- }
- return sortobj;
- };
- const sortArray = function(array) {
- let length = array.length;
- for (let i = 0; i < length; i++) {
- if (typeof array[i] === "object") {
- array[i] = sortObject(array[i]);
- }
- }
- let sortedArray = array.sort();
- return sortedArray;
- };
- console.log(compareObjects(offerA, offerB));
- // WHEN COMPLETE SAVE AS PRIVATE GIST AND INCLUDE THE LINK IN THE WRITTEN INTERVIEW.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement