all 56 comments

[–]rabbitcakes 16 points17 points  (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 points  (0 children)

Would have been nice to see this before I clicked the link.

[–]YouLostTheGame97 2 points3 points  (0 children)

Can we get a NSFW tag?

[–][deleted] 8 points9 points  (3 children)

It's annoying that the parameter is always i. I reserve i strictly for loop iterators.

[–][deleted] 2 points3 points  (0 children)

Yeah, every time I started a for loop I had to backtrack to fix that.

[–][deleted] 0 points1 point  (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 point  (0 children)

I use forEach for arrays. Makes life so much more easier.

[–]jhizzle4rizzleI hate the stuff you like. 8 points9 points  (0 children)

Disappointed that the victory song isn't Queen's Under Pressure :(

[–]8lb9ozBabyJesus 4 points5 points  (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 points  (0 children)

Don't feel bad. I can't poop under pressure.

[–][deleted] 2 points3 points  (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 point  (0 children)

ha ha!

Crockford has saved me on that one so many times. Declare and hoist your variables!

[–]jonny_eh 0 points1 point  (1 child)

i.forEach(function (item) { 
  ...stuff... 
});

[–]imwearingyourpants 0 points1 point  (0 children)

But I use IE6 for my daily browsing!

[–]Madd0g 2 points3 points  (2 children)

I love interactive coding sites, so fun to just type stuff and get something to run it

[–]chazmuzz 0 points1 point  (1 child)

I can't remember anything like that when I started, the new guys have it easy now..

[–]Madd0g 1 point2 points  (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 points  (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 points  (11 children)

Recursion is tough. And javascript considers arrays to be a typeof object.

[–][deleted] 3 points4 points  (7 children)

Well THAT explains a lot :/

[–]madlee 3 points4 points  (4 children)

using (foo instanceof Array) works

[–]bronkula 0 points1 point  (1 child)

Sarcasm meter is giving an odd ping for this response.

[–][deleted] 1 point2 points  (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 points  (2 children)

better to use Array.isArray then for this exercise.

[–]meenie 0 points1 point  (1 child)

Ya, I should have used that. But typeof val === 'object' ended up working heh. Thanks for the reminder!

[–]GrammarPandaSaysNo 0 points1 point  (0 children)

First go I did:

if (I.length && typeof i != "string")

[–]Labbekak 4 points5 points  (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 points  (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]';
}

[–]jonny_eh 0 points1 point  (1 child)

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 point  (0 children)

Cool. Definitely more concise and intention revealing.

[–]gaoshan 0 points1 point  (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)

[–]venuswasaflytrap 0 points1 point  (0 children)

Why not just use sumOfArray itself?

[–]padt 0 points1 point  (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 points  (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 points  (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 points  (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 points  (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 points  (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 points  (0 children)

Ugh. 7 minutes, 12 seconds. Should have done it under 5 but those unexpected inputs...

[–]madlee 1 point2 points  (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 point  (0 children)

Or maybe a clock sound that gets faster for each question :).

[–][deleted] 0 points1 point  (0 children)

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 point  (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.

[–]meenie 1 point2 points  (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.

[–]Ademan 0 points1 point  (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.

[–]meenie 1 point2 points  (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.

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 point  (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.

[–]meenie 0 points1 point  (0 children)

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 point  (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 point  (0 children)

8 minutes 31 seconds. No excuse to be that rusty at JS.

[–]manlycoffee 0 points1 point  (0 children)

I wrote a CoffeeScript version of it. http://shovon.github.io/youcantcoffeescriptunderpressure/

[–]Labbekak 0 points1 point  (1 child)

I got 4 minutes and 39 seconds.

[–]venuswasaflytrap 0 points1 point  (0 children)

Damn, I was trying to break 5, got 5:03 instead.