Skip to content

Comment on You Can't JavaScript Under Pressureparent

Comments

if(typeof i[p] === typeof 3){//it's an int

  sum += i[p];
}else if(typeof i[p] === typeof [3]){//it's an array
  recursion(i[p]);
}

pretty simple no?

That would've been simpler, but I seemed to get away with typeof(i[p]) === "object" and typeof(i[p]) === 1*typeof(i[p]) to test if it was an int.

I dread to think what obscure code I've left behind.

no, in this case the first will match on all numbers and the second will match on all objects.

You should learn a little more about JS before you call this simple.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.