Created
December 14, 2015 14:24
-
-
Save johnboker/494eeb4cf7234db8dcb7 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
var input = [1,"red",5]; | |
var hasRedProperty = function(obj) | |
{ | |
for (var property in obj) | |
{ console.log(property + " " + obj[property]); | |
if(obj[property] === "red") return true; | |
} | |
return false; | |
}; | |
function iterate(obj, sum) | |
{ | |
// uncomment this line for part 2 | |
//if(hasRedProperty(obj) && !(obj instanceof Array)) return sum; | |
for (var property in obj) | |
{ | |
if (obj.hasOwnProperty(property)) | |
{ | |
if (typeof obj[property] == "object") | |
{ | |
sum = iterate(obj[property], sum); | |
} | |
else | |
{ | |
console.log(property + " " + obj[property]); | |
var n = parseInt(obj[property], 10); | |
if(!isNaN(n)) | |
{ | |
sum += n; | |
} | |
} | |
} | |
} | |
return sum; | |
} | |
var sum = iterate(input, 0); | |
console.log("sum: " + sum); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment