Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Written In JavaScript Deep comparison : Does Not Need To Be Recursive The == operator compares objects by identity. But sometimes youd prefer to compare

Written In JavaScript

Deep comparison : Does Not Need To Be Recursive

The == operator compares objects by identity. But sometimes youd 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 whenbotharguments 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 objects 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mechanics Of Materials

Authors: Russell C. Hibbeler

11th Edition

0137605528, 9780137605521

More Books

Students also viewed these Programming questions

Question

Using a 4-bit CLA as the building block, design an 8-bit adder.

Answered: 1 week ago