Created
July 10, 2016 00:39
-
-
Save zoolu-got-rhythm/bd6e41918b3daeaecb3d92f258c5390b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// i took a look at the author's solution after a few hrs and was defintely on the "right track" | |
// but realized there are a ton of ways to do this. but incrementation logic works well, where if there's a match u increment | |
// then u check if both numbers are the same value and call the function parsing the 2 number val's to see if there equal. | |
// where checking for false early in the control flow can save unneccasary computation. | |
function deepEqual(val1, val2){ | |
if(typeof val1 && typeof val2 === "object" && !null){ // change to typeof maybe | |
console.log("ran"); | |
var props1 = 0, props2 = 0; | |
for(var prop in val1){ | |
props1++; | |
} | |
for(var prop in val2){ | |
console.log("looping"); | |
if(!(val1[prop] === val2[prop])){ // if false run additional checks for object and it's property values | |
if((typeof val1[prop] === "object") && (typeof val2[prop] === "object")){ | |
console.log("running additional sub-checks"); | |
var deeperObj2 = val2[prop]; | |
var deeperObj1 = val1[prop]; | |
for(var deeperProp in deeperObj2){ | |
if(!(deeperProp in deeperObj1)) | |
return false; | |
if(!(deeperObj2[deeperProp] === deeperObj1[deeperProp])){ | |
console.log(deeperObj2[deeperProp]); | |
return false; | |
} | |
} | |
}else{ | |
return false; | |
} | |
} | |
// if passes all checks or values are simply equal to each-other increment | |
console.log("incrementing"); | |
props2++; | |
} | |
return deepEqual(props1, props2); | |
}else if(val1 === val2){ | |
console.log("finish"); | |
return true; | |
}else{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment