Hacker News new | ask | show | jobs
by dmak 4639 days ago
I got 11 minutes and 14 seconds. Is that slow? The answer to my last solution was like this:

      function arraySum(i) {
	var total = 0
	for(var x=0; x < i.length; x++) {
		if(typeof i[x] == 'object') {
			 arraySum(i[x])
		} else if(typeof i[x] == 'number') {
			total += i[x]
		}
	}
	return total
     }
3 comments

~6 minutes and 30 seconds

That was also my final answer. However, I was going for speed and not elegance, after looking at other people's responses with prototypes and all.

You must've total+= arraySum(i[x]) right? Otherwise this couldn't work as the result of a deep dive wouldn't be utilized anywhere...
Yeah, I did, thanks for pointing that out. I made that mistake when writing the comment.
you made the same mistake I did at first. Corrected below.

function arraySum(i) { var total = 0 for(var x=0; x < i.length; x++) { if(typeof i[x] == 'object') { total += arraySum(i[x]) } else if(typeof i[x] == 'number') { total += i[x] } } return total }