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

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...
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
Why do we need to use a decrement operator to get it to write the numbers...
Why do we need to use a decrement operator to get it to write the numbers in reverse? Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Ex: If the input is: 5 2 4 6 8 10 #include <iostream> #include <vector> // Must include vector library...
The Problem Below are a series of problems you need to solve using recursive methods BY...
The Problem Below are a series of problems you need to solve using recursive methods BY using java . You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method) DescArrayCheck (15 points) Write a recursive method that checks whether an...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to write an html file (P5.html) that uses JavaScript program to create a Blackjack game. 1. Blackjack Games Rules: a. The object of the game is to "beat the dealer", which can be done in a number of ways: • Get 21 points on your first two cards (called a blackjack), without a dealer blackjack; • Reach a final score higher than the dealer without...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT