News for hackers. Stuff that matters.
Re:Click by skavi on Friday March 19, 2021 @06:37PM (#26518841)
Is that actually the best way to do ranges on JS?
Re:Click by smoe on Friday March 19, 2021 @06:51PM (#26518980)
The 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())
Re:Click by vardaro on Saturday March 20, 2021 @02:25AM (#26521211)
why 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!

Re:Click by epse on Saturday March 20, 2021 @04:48AM (#26521703)
In 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.
Re:Click by eins1234 on Friday March 19, 2021 @08:24PM (#26519563)
I'm a fan of `Array.from({ length: 1000 })`
Re:Click by mahathu on Friday March 19, 2021 @10:32PM (#26520288)
am i missing something with this entire subthread? why not for(let i=0; i<n; ++i){something()};?
Re:Click by topspin on Saturday March 20, 2021 @12:54AM (#26520887)
Doesn't use enough memory perhaps.
Re:Click by momentoftop on Saturday March 20, 2021 @06:59AM (#26522183)
Or if you want to avoid off-by-one errors, why not have a function:

    dotimes(n, () => button.click())
If anyone is in the habit of solving trivial constant space problems in linear space where n is expected to be over 100000, I don't know what to say to them. Are Javascript implementations pretty much guaranteed to optimise the array away?
Re:Click by kbenson on Friday March 19, 2021 @10:53PM (#26520426)
I think it's mostly a stylistic choice, but getting in the habit pays dividends in that it makes it easy to do certain list related actions in a consistent way. forEach doesn't necessarily show this off well, but filter and map make it more obvious.

E.g.

   // Get list of even squares of first 10 integers
    Array.from({ length: 10 }).map((e,i)=>i*i).filter((i)=>i%2==0).forEach((i)=>console.log(i));
    // Or more readable
    Array.from({ length: 10 })
     .map((e,i)=>i*i) // Get squares of each number
     .filter((i)=>i%2==0) // filter for evens
     .forEach((i)=>console.log(i)); // print
Want to filter or manipulate the values in a different way? Throw in another map or filter. Want to pass complex values between steps? You can pack them into an array or object in a map and unpack later.[1] Once you're used to doing stuff with lists, you can use some other interesting list operations like reduce(), some(), every(), etc.

It's not really better (it's subjective), it's just different and a lot of people are used to it and prefer it (and it's fairly consistent in nomenclature across some languages, which is a bonus).

1: https://en.wikipedia.org/wiki/Schwartzian_transform

Re:Click by still_grokking on Saturday March 20, 2021 @12:57PM (#26524383)
Because let transformed = for(let i=0; i<n; ++i){something()}; doesn't work.

For loops are just clunky. They're not values, you can't pass them around or copy'n'paste them where the transformed value is needed.

Re:Click by eins1234 on Monday March 22, 2021 @08:32PM (#26549418)
This. Data and transformations on data (map, filter, reduce, etc) are trivially composable. Imperative loops are not (unless you abstract them away behind a function that accepts and returns data, in which case you'd be re-implementing map, filter, reduce with imperative loops).
Re:Click by xeromal on Friday March 19, 2021 @10:48PM (#26520386)
I guess they can argue they're introducing fewer variables. lol
Re:Click by ficklepickle on Saturday March 20, 2021 @02:22AM (#26521193)
there is no true scotsman

[...Array(n)].map(something)

Re:Click by vardaro on Saturday March 20, 2021 @02:26AM (#26521217)
this is what i’m preaching
Re:Click by halfmatthalfcat on Friday March 19, 2021 @06:40PM (#26518881)
Traditional 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.
Re:Click by franky47 on Friday March 19, 2021 @06:45PM (#26518923)
The functional approach also works better as a one-liner when typed in the devtools console.
Re:Click by antihero on Saturday March 20, 2021 @12:02AM (#26520728)
I guess you could do `[...new Array(1000)].map((_,i) => i);` to create a sequence.