function - Javascript - deepEqual Comparison and Recursion -


i'm teaching myself javascript, , i'm stuck on following problem eloquent javascript:

write function, deepequal, takes 2 values , returns true if same value or objects same properties values equal when compared recursive call deepequal. find out whether compare 2 things identity (use === operator that) or looking @ properties, can use typeof operator. if produces "object" both values, should deep comparison. have take 1 silly exception account: historical accident, typeof null produces "object".

my question: why console.log(deepequal(obj, {here: {is: "an"}, object: 2})) come out false when a[key] { is: 'an' } , b[key] { is: 'an' } true when code substituted !deepequal(a[key], b[key])?

the code:

function deepequal(a, b){     if (a === b) {         return true;     }      else if (typeof a== typeof b && !(a===null ||b===null)){         for(var key in b){             for(key in a){                 if(! (key in a)){                     return false;                 }                  else if (a[key] !== b[key]){                     return false;                 }                 else                      return true;             }         }     }   else       return false; }  var obj = {here: {is: "an"}, object: 2}; console.log(deepequal(obj, {here: {is: "an"}, object: 2})); 

because a[key] , b[key] both objects, need use deepequals on them well.

currently, you're not calling deepequals recursively, there's no way happen.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -