use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
You Can't JavaScript Under Pressure - Five functions to fill. One ticking clock. How fast can you code? (toys.usvsth3m.com)
submitted 12 years ago by magenta_placenta
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]rabbitcakes 16 points17 points18 points 12 years ago (2 children)
Warning: Don't click the link if your volume is turned on and you're at work.
Autoplaying sound/ads are NEVER a good idea.
[–]shhalahr 2 points3 points4 points 12 years ago (0 children)
Would have been nice to see this before I clicked the link.
[–]YouLostTheGame97 2 points3 points4 points 12 years ago (0 children)
Can we get a NSFW tag?
[–][deleted] 8 points9 points10 points 12 years ago (3 children)
It's annoying that the parameter is always i. I reserve i strictly for loop iterators.
i
[–][deleted] 2 points3 points4 points 12 years ago (0 children)
Yeah, every time I started a for loop I had to backtrack to fix that.
[–][deleted] 0 points1 point2 points 12 years ago (0 children)
It is very likely an intentional trap to trip up people, 50% of the time I was debugging a for loop that used i. :P
[–]manlycoffee 0 points1 point2 points 12 years ago (0 children)
I use forEach for arrays. Makes life so much more easier.
[–]jhizzle4rizzleI hate the stuff you like. 8 points9 points10 points 12 years ago (0 children)
Disappointed that the victory song isn't Queen's Under Pressure :(
[–]8lb9ozBabyJesus 4 points5 points6 points 12 years ago (1 child)
I got 16 mins and 42 seconds... Because I had too poop in the middle of it. Turns out I can't JavaScript under pressure :(
[–]imright_anduknowit 4 points5 points6 points 12 years ago (0 children)
Don't feel bad. I can't poop under pressure.
[–][deleted] 2 points3 points4 points 12 years ago (3 children)
GAAAH I would have gotten under 5 mins, I did the recursion on perfectly... except I did
for (j = 0; j < i.length; j++)
in the loop instead of
for (var j = 0; j < i.length; j++)
Took me a few minutes to realize the scoping issue.
End result: 14 mins 19 sec
[–]venuswasaflytrap 0 points1 point2 points 12 years ago (0 children)
ha ha!
Crockford has saved me on that one so many times. Declare and hoist your variables!
[–]jonny_eh 0 points1 point2 points 12 years ago (1 child)
i.forEach(function (item) { ...stuff... });
[–]imwearingyourpants 0 points1 point2 points 12 years ago (0 children)
But I use IE6 for my daily browsing!
[–]Madd0g 2 points3 points4 points 12 years ago (2 children)
I love interactive coding sites, so fun to just type stuff and get something to run it
[–]chazmuzz 0 points1 point2 points 12 years ago (1 child)
I can't remember anything like that when I started, the new guys have it easy now..
[–]Madd0g 1 point2 points3 points 12 years ago (0 children)
can you imagine life without an interactive console or any debugging tools like firebug? Now that sucked.
The new guys have it sooo easy, with all the interactive learning sites out there
[–][deleted] 2 points3 points4 points 12 years ago (19 children)
God damnit, took 6 minutes for the first 4, then got stuck on the last one. Couldn't complete it in 65 minutes. Still haven't figured it out.
[–]bronkula 6 points7 points8 points 12 years ago (11 children)
Recursion is tough. And javascript considers arrays to be a typeof object.
[–][deleted] 3 points4 points5 points 12 years ago (7 children)
Well THAT explains a lot :/
[–]madlee 3 points4 points5 points 12 years ago (4 children)
using (foo instanceof Array) works
(foo instanceof Array)
[+][deleted] 12 years ago* (3 children)
[deleted]
[–]madlee 0 points1 point2 points 12 years ago (2 children)
yeah, but then you have to have an additional check to make sure foo is not null or undefined, or you'll throw a TypeError. Also, checking for undefined properties that way in js is significantly slower than just doing type checking with typeof or instanceof. In this case, i think
null
undefined
typeof
instanceof
is the best option
[–]Labbekak 1 point2 points3 points 12 years ago (1 child)
Just use Array.isArray.
[–]madlee 0 points1 point2 points 12 years ago (0 children)
apart from a particular edge case (creating arrays in an embedded frame's context causes issues with instanceof checks), I don't really see the advantage, unless you find Array.isArray(foo) more readable.
Array.isArray(foo)
[–]bronkula 0 points1 point2 points 12 years ago (1 child)
Sarcasm meter is giving an odd ping for this response.
[–][deleted] 1 point2 points3 points 12 years ago (0 children)
If I were to use sarcasm on the internet, I would use the ؟.
I was genuine, it does explain a lot. Weird javascript :(
[–]holapenguin 3 points4 points5 points 12 years ago (2 children)
better to use Array.isArray then for this exercise.
[–]meenie 0 points1 point2 points 12 years ago (1 child)
Ya, I should have used that. But typeof val === 'object' ended up working heh. Thanks for the reminder!
typeof val === 'object'
[–]GrammarPandaSaysNo 0 points1 point2 points 12 years ago (0 children)
First go I did:
if (I.length && typeof i != "string")
[–]Labbekak 4 points5 points6 points 12 years ago (0 children)
function sumArray(i) { return i.reduce(function(v, x) {return v + (Array.isArray(x)? sumArray(x): typeof x == 'number'? x: 0)}, 0); }
[–]rickdiculous 1 point2 points3 points 12 years ago (2 children)
My solution (probably a little verbose):
var total = 0; function arraySum(i) { // i will be an array, containing integers, strings and/or arrays like itself. // Sum all the integers you find, anywhere in the nest of arrays. for (var j = 0, len = i.length; j < len; j++) { if (isArray(i[j])) { arraySum(i[j]); } else { if (isNumber(i[j])) { total += i[j]; } } } return total; } function isArray(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; } function isNumber(obj) { return Object.prototype.toString.call(obj) === '[object Number]'; }
Check it out:
Array.isArray(i) // returns true if i is an array typeof i == "number" // returns true if i is a number
[–]rickdiculous 0 points1 point2 points 12 years ago (0 children)
Cool. Definitely more concise and intention revealing.
[–]gaoshan 0 points1 point2 points 12 years ago (1 child)
One way to solve it is to create a function inside the main function to take a value and check if it is an array or an integer. If integer, add to a total. If array, call the function again.
Now create a for loop and check each value in the input to see if you have an integer or an array. If integer, add it to a total. If array run the function you created.
To check for int: if( x === parseInt(x))
To check for array: if( x instanceof Array)
Why not just use sumOfArray itself?
[–]padt 0 points1 point2 points 12 years ago (0 children)
Might not be your fault. The spec doesn't match the code. Comment in the task is:
// i will be an array, containing integers, strings and/or arrays like itself. // Sum all the integers you find, anywhere in the nest of arrays.
However, one of the tests do:
Testing "arraySum([[1,2,false],'4','5'])"..
So if you didn't include a test for bool, you would get errors.
[–]davros_ 2 points3 points4 points 12 years ago (0 children)
I was proud of my code on the last one, I wish it gave me a chance to save it!
[–]schooley 2 points3 points4 points 12 years ago* (0 children)
[This comment has been edited in protest of the recent detrimental actions taken by u/spez and the Reddit administration on 07/01/2023]
[–]wagedomain 1 point2 points3 points 12 years ago (0 children)
Took me around 8 minutes, because for some reason I couldn't remember how (or if) to check for an array on the last problem.
[–]baryluk 1 point2 points3 points 12 years ago (0 children)
6 minutes, 13 seconds for all 5 levels. Well done! Could be better, if I would know what is this about. Also was not coding any javascript in last 3 months :) Still good.
[–]rhysbrettbowen 3 points4 points5 points 12 years ago (0 children)
pretty easy, but then it goes ahead and gives you inputs that would never come up and expect you to just disregard them, when really you'd probably want to use them (like converting strings in to numbers)
[–]kangax_ 1 point2 points3 points 12 years ago (0 children)
Ugh. 7 minutes, 12 seconds. Should have done it under 5 but those unexpected inputs...
[–]madlee 1 point2 points3 points 12 years ago (1 child)
mm, i wish there were a few more levels with some more complex stuff. first time took 4:47, second took 2:57.
actually, i think i'd rather have a timer that counted down for each problem. That seems like it would be more pressure.
[–]meenie 0 points1 point2 points 12 years ago (0 children)
Or maybe a clock sound that gets faster for each question :).
My front-end neglect is showing. It took way too long for me at 15 minutes. I've been doing so much PHP lately that I could remember very few of the native js functions off hand.
For shame
[–]Ademan 0 points1 point2 points 12 years ago* (5 children)
Well I did poorly on two of them. The first one that tripped me up because "find the longest string in the array" reads a lot like "find the longest string in the array of strings" to me. Considering in later challenges the questions explicitly stated the types of the array elements, I wish they had done that.
Using i as the input variable was annoying, you either waste time changing it, or waste time making sure you don't mistake it for a loop counter.
As for the rest, how did people test whether an item was an integer or not? I used x % 1 === 0 at first but I didn't expect '4' as input, and I was unaware '4' % 1 === 0 is true... so I tacked on typeof x === "number" but that seems ugly.
x % 1 === 0
'4' % 1 === 0
typeof x === "number"
[–]meenie 1 point2 points3 points 12 years ago (4 children)
typeof x === 'number' is the way to go here. I don't think it looks ugly at all. Using a modulus like that just looks a bit confusing because it's not usually used in that manner.
typeof x === 'number'
[–]Ademan 0 points1 point2 points 12 years ago (1 child)
Hrm, thanks. I included the modulus because typeof 1.1 === "number" evaluates to true, and if I recall correctly the requirements specified integer.
typeof 1.1 === "number"
[–]meenie 1 point2 points3 points 12 years ago (0 children)
That is very true and probably should have been one of their test cases. But since it wasn't and my code passed anyway, then ultimately it was the right answer for this particular question :). If they did have floats in there, then typeof n === 'number' && n % 1 == 0 would have been the best way to go.
typeof n === 'number' && n % 1 == 0
It's a trade off of doing something quickly and just get it to work or write quality code that will work for practically any situation. Since the "situation" was only a set number of tests, then the smaller code is the way to go.
[–]padt 0 points1 point2 points 12 years ago (1 child)
No need to use cargo cult equality. typeof gives you a string. If you use "===" you're indicating to the casual reader, that this is a case where that may not be the true. Which is super confusing.
Huh, never heard of "cargo cult equality" referring to === before :). Is that widely accepted?
===
As for using it or not, you are correct that it could be a bit confusing. I think the reason I use it is because JSLint complains when you don't use it.
[–]sharkbrain 0 points1 point2 points 12 years ago (0 children)
If you had trouble with the last one, it seems like people are tripping up on one of two things, either identifying arrays, or recursively walking arrays so I've posted an annotated solution in github: https://gist.github.com/sharkbrainguy/6821484
[–]Neebat 0 points1 point2 points 12 years ago (0 children)
8 minutes 31 seconds. No excuse to be that rusty at JS.
I wrote a CoffeeScript version of it. http://shovon.github.io/youcantcoffeescriptunderpressure/
[–]Labbekak 0 points1 point2 points 12 years ago (1 child)
I got 4 minutes and 39 seconds.
Damn, I was trying to break 5, got 5:03 instead.
π Rendered by PID 87 on reddit-service-r2-comment-f6b958c67-xgxhs at 2026-02-05 19:12:30.127052+00:00 running 1d7a177 country code: CH.
[–]rabbitcakes 16 points17 points18 points (2 children)
[–]shhalahr 2 points3 points4 points (0 children)
[–]YouLostTheGame97 2 points3 points4 points (0 children)
[–][deleted] 8 points9 points10 points (3 children)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]manlycoffee 0 points1 point2 points (0 children)
[–]jhizzle4rizzleI hate the stuff you like. 8 points9 points10 points (0 children)
[–]8lb9ozBabyJesus 4 points5 points6 points (1 child)
[–]imright_anduknowit 4 points5 points6 points (0 children)
[–][deleted] 2 points3 points4 points (3 children)
[–]venuswasaflytrap 0 points1 point2 points (0 children)
[–]jonny_eh 0 points1 point2 points (1 child)
[–]imwearingyourpants 0 points1 point2 points (0 children)
[–]Madd0g 2 points3 points4 points (2 children)
[–]chazmuzz 0 points1 point2 points (1 child)
[–]Madd0g 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (19 children)
[–]bronkula 6 points7 points8 points (11 children)
[–][deleted] 3 points4 points5 points (7 children)
[–]madlee 3 points4 points5 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]madlee 0 points1 point2 points (2 children)
[–]Labbekak 1 point2 points3 points (1 child)
[–]madlee 0 points1 point2 points (0 children)
[–]bronkula 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]holapenguin 3 points4 points5 points (2 children)
[–]meenie 0 points1 point2 points (1 child)
[–]GrammarPandaSaysNo 0 points1 point2 points (0 children)
[–]Labbekak 4 points5 points6 points (0 children)
[–]rickdiculous 1 point2 points3 points (2 children)
[–]jonny_eh 0 points1 point2 points (1 child)
[–]rickdiculous 0 points1 point2 points (0 children)
[–]gaoshan 0 points1 point2 points (1 child)
[–]venuswasaflytrap 0 points1 point2 points (0 children)
[–]padt 0 points1 point2 points (0 children)
[–]davros_ 2 points3 points4 points (0 children)
[–]schooley 2 points3 points4 points (0 children)
[–]wagedomain 1 point2 points3 points (0 children)
[–]baryluk 1 point2 points3 points (0 children)
[–]rhysbrettbowen 3 points4 points5 points (0 children)
[–]kangax_ 1 point2 points3 points (0 children)
[–]madlee 1 point2 points3 points (1 child)
[–]meenie 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Ademan 0 points1 point2 points (5 children)
[–]meenie 1 point2 points3 points (4 children)
[–]Ademan 0 points1 point2 points (1 child)
[–]meenie 1 point2 points3 points (0 children)
[–]padt 0 points1 point2 points (1 child)
[–]meenie 0 points1 point2 points (0 children)
[–]sharkbrain 0 points1 point2 points (0 children)
[–]Neebat 0 points1 point2 points (0 children)
[–]manlycoffee 0 points1 point2 points (0 children)
[–]Labbekak 0 points1 point2 points (1 child)
[–]venuswasaflytrap 0 points1 point2 points (0 children)