Note: All of these techniques assign the alphabet string to variable a
.
I am 99% certain that the shortest way to achieve this in JavaScript is indeed:
a="abcdefghijklmnopqrstuvwxyz" // 30 bytes
But there are several other interesting methods. You can use string compression:
a=btoa`i·?yø!?9%?z)ª»-ºü1`+'yz' // 31 bytes; each ? represents an unprintable
You can get the compressed string from atob`abcdefghijklmnopqrstuvwx`
. The 'yz'
must be added manually because if you compress the whole string, while the result is only 27 bytes, it will turn out as abcdefghijklmnopqrstuvwxyw==
.
I believe the shortest way to do it programmatically is also the method you suggested:
for(i=97,a='';i<123;)a+=String.fromCharCode(i++) // 48 bytes
You can do it with ES6 features (template strings ``
, spread operator ...
) if you want:
a=[...Array(26)].map(_=>String.fromCharCode(i++),i=97).join`` // 61 bytes
a=[...Array(26)].map((_,i)=>String.fromCharCode(i+97)).join`` // also 61 bytes
a=[...Array(i=26)].map(_=>String.fromCharCode(++i+70)).join`` // again, 61 bytes
You can do one better with a variable instead of .join``
:
[...Array(26)].map(_=>a+=String.fromCharCode(i++),i=97,a='') // all 60 bytes
[...Array(26)].map((_,i)=>a+=String.fromCharCode(i+97),a='')
[...Array(i=26)].map(_=>a+=String.fromCharCode(++i+70),a='')
Or ES7 with array comprehensions, which is another byte shorter:
a=[for(_ of Array(i=26))String.fromCharCode(++i+70)].join`` // 59 bytes
Creating the variable beforehand saves yet another byte:
a='',[for(_ of Array(i=26))a+=String.fromCharCode(++i+70)] // 58 bytes
Also, String.fromCharCode
accepts multiple arguments and will automatically join them. So we can golf each ES6 version down to 57 bytes:
a=String.fromCharCode(...[...Array(26)].map(_=>i++,i=97)) // all 57 bytes
a=String.fromCharCode(...[...Array(26)].map((_,i)=>i+97))
a=String.fromCharCode(...[...Array(i=26)].map(_=>++i+70))
And the ES7 one down to 55:
a=String.fromCharCode(...[for(_ of Array(i=26))++i+70]) // 55 bytes
If you'd like to learn more about golfing ranges, check out this set of tips. There's also one about ES7's array comprehensions.
EDIT: As edc65 has pointed out, most of these become shorter using i.toString(36)
instead of String.fromCharCode(i)
:
for(i=9,a='';++i<36;)a+=i.toString(36) // 38 bytes
a=[...Array(26)].map(_=>(++i).toString(36),i=9).join`` // 54 bytes
[...Array(26)].map(_=>a+=(++i).toString(36),i=9,a='') // 53 bytes
i=9,a=[for(_ of Array(26))(++i).toString(36)].join`` // 52 bytes
i=9,a='',[for(_ of Array(26))a+=(++i).toString(36)] // 51 bytes
I believe this one is the shortest possible that can be called as a function return value:
eval("for(i=9,a='';++i<36;)a+=i.toString(36)") // 46 bytes
It's three bytes shorter than manually returning it from a function:
x=>eval("for(i=9,a='';++i<36;)a+=i.toString(36)") // 49 bytes
x=>{for(i=9,a='';++i<36;)a+=i.toString(36);return a} // 52 bytes
Of course, x=>"abcdefghijklmnopqrstuvwxyz"
still beats everything else.
11
@muddyfish, LuisMendo: This is on-topic per meta.
– Doorknob – 2016-02-09T21:11:34.5471
[...Array(26)].map((q,w)=>String.fromCharCode(w+97))
is 52 bytes and add another 7 for the.join\
`` – andlrc – 2016-02-09T21:13:42.623Related: http://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-an-array-based-on-suppl
– Digital Trauma – 2016-02-09T21:19:21.543@dev-null a='';i=97;[...Array(26)].map(b=>a+=String.fromCharCode(i++)) is 60 but takes care of the join how are you doing join in 7 without getting commas in the result? – Charlie Wynn – 2016-02-09T21:19:46.170
1@CharlieWynn
[...Array(26)].map((q,w)=>String.fromCharCode(w+97)).join\
`` – andlrc – 2016-02-09T21:23:49.887@dev-null join`` is new to me! – Charlie Wynn – 2016-02-09T21:25:20.203