4 ms·
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.le
by dmak 13y 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
}
- lighthazard 13y ago~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.
- dclowd9901 13y agoYou must've total+= arraySum(i[x]) right? Otherwise this couldn't work as the result of a deep dive wouldn't be utilized anywhere...
- dmak 13y agoYeah, I did, thanks for pointing that out. I made that mistake when writing the comment.
- ep103 13y agoyou 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 }