Live data from Hacker News

You Can't JavaScript Under Pressure

toys.usvsth3m.com

71–80 of 110 posts

Re: You Can't JavaScript Under Pressure

#71
post #49

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

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 }

Re: You Can't JavaScript Under Pressure

#73
post #45

nice stuff but this is plain wrong! // return the file extension (with no period) if it has one, otherwise false its like returning error codes in C for errors

I too shuddered at this instruction...

Or for using i as your input variable

Re: You Can't JavaScript Under Pressure

#74
post #64
post #42

It's problems like 5 that make me sad that IE return i.reduce(function(prev, next) { if (typeof(next) == "object" && next.length) { return arraySum(next) + prev; } if (typeof(next) == "number") { return next + prev; } return prev; }, 0); Something that simple needs to be shimmed on earlier IEs. In fact, I had to look up Array.prototype.reduce for this since I usually use Underscore's. Javascript problems...

https://github.com/kriskowal/es5-shim

Cool project, but all the warnings on that page about the edge cases make me concerned about trying to exuberantly move to ES5, and so I just stick with Underscore and the same level of JavaScript syntax I've been writing since 2005. (sigh)

Re: You Can't JavaScript Under Pressure

#75
post #16

Fun little puzzles. First 4 went pretty quickly (~8:00) and then the last one took ~7 minutes Googling around for this so I could test if a variable was an integer... n===+n && n===(n|0) Wish I could see my answers after the fact though! Edit: Read the initial requirements of the puzzle, it says sum all the integers , not all the numbers . People using (typeof i[x] == 'number') just got lucky because the test-cases d…

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?

Re: You Can't JavaScript Under Pressure

#77
post #56
post #42

It's problems like 5 that make me sad that IE return i.reduce(function(prev, next) { if (typeof(next) == "object" && next.length) { return arraySum(next) + prev; } if (typeof(next) == "number") { return next + prev; } return prev; }, 0); Something that simple needs to be shimmed on earlier IEs. In fact, I had to look up Array.prototype.reduce for this since I usually use Underscore's. Javascript problems...

Pretty interesting solution. The first thing that came to my head was a loop, but you just used reduce. What are some scenarios where you would use a reduce over a loop? I use underscore a lot too, and I find myself just using _.each all the time

Anytime where you are trying to "collect" features of an array into one final value, and the collection function can be sensibly applied to the elements in any order as long as some interim value is passed along (e.g. summing, maximizing), the reduce paradigm is appropriate and can be more readable.

The bonus upside is that these paradigms also make parallelism quite simple, should it later be necessary.

Re: You Can't JavaScript Under Pressure

#78
post #5

Thanks for immediately playing audio without asking me first. I know everybody just loves it when web pages do that.

If using chrome, the Chrome Toolbox has a handy mute all tabs feature. I have it muted by default. https://code.google.com/p/chrome-toolbox/

That's great! Here is the actual plugin I had to look up: https://chrome.google.com/webstore/detail/chrome-toolbox-by-...

Re: You Can't JavaScript Under Pressure

#79

I'm a bit disappointed that it doesn't award points for my heavy use of regex and obscure variable names.

> obscure variable names

Absolutely! Being under the gun led me, at one point, to have a "longest string in array" method with a statement like

  if (i[z].length 

Re: You Can't JavaScript Under Pressure

#80
post #16

Fun little puzzles. First 4 went pretty quickly (~8:00) and then the last one took ~7 minutes Googling around for this so I could test if a variable was an integer... n===+n && n===(n|0) Wish I could see my answers after the fact though! Edit: Read the initial requirements of the puzzle, it says sum all the integers , not all the numbers . People using (typeof i[x] == 'number') just got lucky because the test-cases d…

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.

Post reply on HN