9 ms·
This is great. I tried to hack the progress by automating clicks, using: let button = document.getElementsByClassName('button')[0] Array(100000).fill(undef
by franky47 6y ago
This is great. I tried to hack the progress by automating clicks, using:
let button = document.getElementsByClassName('button')[0]
Array(100000).fill(undefined).forEach(() => button.click())
In response, I got the following log message:
> Such a smart subject.
- johnfn 6y agoWell, that's an unconventional way to write a for loop. Maybe the website is commending you on that :-)
- notJim 6y agoThis is your brain on javascript :) Guessing it's bc one-liners are easier to write than for loops in the console.
- mrcartmenez 6y agoIt’s also because it’s good practice to use the lowest power loop possible. A for loop can implement a reduce, a reduce can implement a map, a map can implement a forEach, but not the inverse.
- thaumasiotes 6y agoHuh? I don't follow. In [1]: def lred(function, iterable, initializer): ...: acc = initializer ...: def reductor(el): ...: nonlocal acc ...: acc = function(acc, el) ...: for n in map(reductor, iterable): pass ...: return acc ...: In [2]: lred(lambda a,b: 10*a + b, [2,4,6,3,1], 40) Out[2]: 4024631 What's stopping you from running these equivalencies in either direction?
- jessedhillon 6y agoThey mean that the forEach runs code per element, but cannot return a transformed list of values, you need map for that. A map can run code per list element but the result must be injective (one-to-one). Reduce can do all of the above, but must return a lower dimension result from its input. And finally, a for loop is basically omnipotent.
- DangitBobby 6y ago> Reduce can do all of the above, but must return a lower dimension result from its input. What do you mean by this? You can accumulate just about anything into the resulting object, including a copy of the original array.
- SamBam 6y agoI think it's just being confusingly stated. GP already said that reduce could implement map. I think it's merely that reduce can just output a single result, of any type. (Which is already more powerful than map, which must output an array.) Of course, in fact you could hack anything in any of these, since you could be doing other work in the function called. But I think the general principle is sound.
- thaumasiotes 6y ago> (Which is already more powerful than map, which must output an array.) Don't tell Common Lisp.
- eru 6y agoEspecially, a for-loop can break out early or skip elements.
- thaumasiotes 6y agoSo can reduce. You'd put that logic in the function you pass to the call to reduce. So can map. Ditto. So can foreach. Ditto again. for el in values: if el == 3: pass else: do_something_with( el )
- vsareto 6y ago>Guessing it's bc one-liners are easier to write than for loops in the console. Firefox has a pretty good multi-line editor now if anyone was looking for that
- hahajk 6y agoI haven’t had an off-by-one error since I stopped using for loops.
- halfmatthalfcat 6y agoCan save more bits by doing: Array.from({ length: 10000 }, button.click);
- phogster 6y agoNever heard of a "from" loop before.
- franky47 6y agoArray.from lets you build an array from an iterable source (often used in conjunction with `new Set` to eliminate duplicates), or in this case, of a fixed length.
- deleted 6y ago[deleted]
- klyrs 6y agoNot JavaScript, but... https://en.m.wikipedia.org/wiki/COMEFROM https://en.m.wikipedia.org/wiki/COMEFROM
- franky47 6y agoI did not know this variant, thanks. Having to fill the array with anything (even `undefined`) to iterate on it seemed such a weird concept (but then again, JS is weird).
- anonu 6y agoIt's how you would do it with a language like APL or q.
- makach 6y agoIt responded correctly, it said: Robot, Exciting and then wrote "subject has run script to click on the button ten times within one second" whereafter "subject has clicked on the button a thousand times" Maybe a contender to CookieClicker? 73% achievements atm
- skavi 6y agoIs that actually the best way to do ranges on JS?
- halfmatthalfcat 6y agoTraditional for loops are probably the most common for ranges but if you want a more functional approach you can use the Array constructor or Array.prototype.from with its various parameters.
- franky47 6y agoThe functional approach also works better as a one-liner when typed in the devtools console.
- smoe 6y agoThe other comment points out Array.from, which seems pretty nifty. Have not seen it before. I would have used: for(i of Array(1000).keys()) { doSomething() } or [...Array(1000)].forEach(() => doSomething())
- vardaro 6y agowhy create the array? for(let i = 0; i < 1000; i++) button.click() wouldn’t be surprised if Javascript has some weird optimization under the hood!
- epse 6y agoIn this case, there's not much difference. In general, iterating over something rather than using a counter with limits makes it harder to create off by one or other errors due to laziness.
- eins1234 6y agoI'm a fan of `Array.from({ length: 1000 })`
- NaturalPhallacy 6y agoYou and everyone else copying it seems to have DDOSd the site and now I don't get to play with it lol
- franky47 6y agoUnless clicking the button furiously sends ton of network requests (I'm on my phone so can't confirm ATM), the only thing you're DoSing is your own CPU, as I forgot to add some kind of thread relief delay.
- Waterluvian 6y agoJavascript has gotten so good with ES6 and beyond. But there's still no nice way to do something n times without a C style loop.
- BeefWellington 6y agoxdotool can do this type of thing pretty easily as well: Open a console, and prep: xdotool click --delay 50 --repeat 1000 1 Move your mouse to the location and press enter.
- RMPR 6y agoShameless plug, you can do the same with this[0] albeit not in the CLI. 0: https://github.com/rmpr/atbswp https://github.com/rmpr/atbswp
- vippy 6y ago`setTimeout`, friend ;)
- ericmcer 6y agoI used a `setInterval(() => btn.click(),100)` and that seemed to fool it
- deleted 6y ago[deleted]