by spinlock · 11y ago · 0 comments
Also, it's nice to work in a language that has the right features and not just all the features. I like CoffeeScript as much for what it doesn't have as for what it does have. I recently saw this piece of code:
new Array(26)
.map(function (item, index) {
return String.fromCharCode(65 + index);
})
as a "heads up" for map lovers. That's the kind of code that people write when they're tacking the latest new feature onto someplace that it doesn't belong. This code isn't just semantically wrong, it's stylistically wrong. Having a less schizophrenic language helps jr guys avoid code like the above. 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))