by jsprogrammer · 11y ago · 0 comments
const caps = new Array(26).map((item, index) => String.fromCharCode(65 + index));
It's not wrong. Could maybe be better though. const caps = new Array(26).map((item, index) => String.fromCharCode(65 + index));
Shorter, easier to reason against. const caps = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');Someone will probably chime in here with a correction, but the point is this -- JS is NOT a friendly language to deal with.
new Array(26).length === 26; //trueJSBin: http://jsbin.com/vasulumeti/1/edit?js,console
StackOverflow: http://stackoverflow.com/a/5501711/211291
For example: [1,2,3,undefined].forEach(ele => console.log(ele)) > 1 > 2 > 3 > undefined On the other hand: [1,2,3,,].forEach(ele => console.log(ele)) > 1 > 2 > 3
So, take the time you dedicate to hating JavaScript to understand the language instead of bitching here you and that stupid jerk above.
[65..90].map (number) -> String.fromCharCode(number)
where you're operating on the elements of your array and you don't care about their index.Because it's the shortest way to do it in that language.
I think something like:
[A-Z].reverse
or: reverse([A-Z])
might even be better. Do you know of a language with that syntax? for (1 = 65; i <= 90; i++){...}
It's not like the syntax is that bad. But, iterators are simpler than indexing a collection. Iterators work with data structures like linked lists where you don't really have an index. And, they work when the collection changes while you're iterating it. Array.apply(null, Array(26)).map((_, index) => String.fromCharCode(65 + index))