Question

In: Computer Science

Written In JavaScript Deep comparison : Does Not Need To Be Recursive The == operator compares...

Written In JavaScript

Deep comparison : Does Not Need To Be Recursive

The == operator compares objects by identity. But sometimes you’d prefer to compare the values of their actual properties.

Write a function deepEqual that takes two values and returns true only if they are the same value or are objects with the same properties, where the values of the properties are equal when compared with a recursive call to deepEqual.

To find out whether values should be compared directly (use the === operator for that) or have their properties compared, you can use the typeof operator. If it produces "object" for both values, you should do a deep comparison. But you have to take one silly exception into account: because of a historical accident, typeof null also produces "object".

The Object.keys function will be useful when you need to go over the properties of objects to compare them.

// Your code here.

let obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// → true

Your test for whether you are dealing with a real object will look something like typeof x == "object" && x != null. Be careful to compare properties only when both arguments are objects. In all other cases you can just immediately return the result of applying ===.

Use Object.keys to go over the properties. You need to test whether both objects have the same set of property names and whether those properties have identical values. One way to do that is to ensure that both objects have the same number of properties (the lengths of the property lists are the same). And then, when looping over one of the object’s properties to compare them, always first make sure the other actually has a property by that name. If they have the same number of properties and all properties in one also exist in the other, they have the same set of property names.

Returning the correct value from the function is best done by immediately returning false when a mismatch is found and returning true at the end of the function.

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

function deepEqual(obj1, obj2) {
// first check whether the type of obj1 and obj2 are same. If not, return false.
if(obj1 != null && obj2 != null && typeof obj1 == "object" && typeof obj2 == "object") {
let obj1Length = Object.keys(obj1).length;
let obj2Length = Object.keys(obj2).length;

// then, compare the length of obj1 and obj2 properties. If they are not equal, return false.
if(obj1Length === obj2Length) {
let isEqual = true;

// Then, loop to go through each property in obj1
for(var prop in obj1) {

// if the property exist in obj2, then go through each of the property and compare their values
  
if(obj2[prop] != null) {
// isEqual will store the result of deepEqual of every properties in obj1 and obj2
isEqual = isEqual && deepEqual(obj1[prop], obj2[prop]);
} else {
// the property of obj1 does not exist in obj2, return false.
return false;
}
}
// return isEqual
return isEqual;
} else {

// the length og obj1 and obj2 properties are not same, return false.
return false;
}
} else {

// if obj1 and obj2 are not properties, then do === to compare them.
return obj1 === obj2;
}

// return true
return true;
}

  
var obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// ? true
console.log(deepEqual(obj, {here: 1, object: 2}));
// ? false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// -> true

Related Solutions

This needs to be written in JavaScript with two files. One HTML file and a JavaScript...
This needs to be written in JavaScript with two files. One HTML file and a JavaScript file. Make an empty HTML file, put three buttons inside the body and an empty main tag below the buttons. The text of the buttons should be "Foo", "Bar", and "FooBar" respectively. Don't put the " in the buttons that's just for clarification in these instructions. Add unique IDs to each button In your JavaScript, get 3 separate references to the buttons using the...
NOTE: This problem does NOT need more information. All the information is here directly as written...
NOTE: This problem does NOT need more information. All the information is here directly as written from the book. I have most done just needed some help with setting up in excel and solver. Again, it has all the information. Help Solve this problem please, then use EXCEL to format this and solve using solve and explain. Biggest problem is once have variables (which i have half done) is setting up in Excel. A farmer in the Midwst haas 1,000...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
I need to design and implement a JAVASCRIPT program that will allow us to determine the...
I need to design and implement a JAVASCRIPT program that will allow us to determine the length of time needed to pay off a credit card balance, as well as the total interest paid. The program must implement the following functions: displayWelcome This function should display the welcome message to the user explaining what the program does. calculateMinimumPayment This function calculates the minimum payment. It should take balance and minimum payment rate as arguments and return the minimum payment. So...
you will need to complete a paper of a minimum 500 words that compares and contrasts...
you will need to complete a paper of a minimum 500 words that compares and contrasts the Enterprise DCF valuation methodology with the Economic Profit valuation methodology. Explain the differences in the two approaches and the benefits and downsides of each.
In need of a soild conclusion that compares and contrasts survey, experimental, and field research.
In need of a soild conclusion that compares and contrasts survey, experimental, and field research.
Research publicly traded companies and select two companies in different sectors. Provide a written comparison of...
Research publicly traded companies and select two companies in different sectors. Provide a written comparison of the capital structure for each. Explain your conclusions on the similarities and differences. What factors can you suggest for why each company adheres to their chosen structuring mechanism?
For this assignment you need to create a ToDo list using Javascript, along with HTML and...
For this assignment you need to create a ToDo list using Javascript, along with HTML and CSS. Begin by creating a HTML page called todo.html. Then create a Javascript file called todo.js and link it in to the HTML page using a script tag. All Javascript for the assignment must be in the separate file. (For CSS, feel free to include styles in a style block at the top of the HTML page, or to link in CSS from a...
For this assignment you need to create a ToDo list using Javascript, along with HTML and...
For this assignment you need to create a ToDo list using Javascript, along with HTML and CSS. Begin by creating a HTML page called todo.html. Then create a Javascript file called todo.js and link it in to the HTML page using a script tag. All Javascript for the assignment must be in the separate file. (For CSS, feel free to include styles in a style block at the top of the HTML page, or to link in CSS from a...
i need code in javascript or htmlt convert 0 to 999 numbers into word
i need code in javascript or htmlt convert 0 to 999 numbers into word
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT