Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.