Generating the alphabet in JavaScript

  • I'm pretty sure there's not a better way to do this but figured it couldn't hurt to ask.



    I'm tired of typing out a='abcdefghijklmnopqrstuvwxyz'.



    Cool languages have Range('a'..'z') or similar



    What can we come up with with JS that's as short as possible??



    for(i=97,a='';i<123;){a+=String.fromCharCode(i++)}


    is longer than just the alphabet - but guarantees I don't screw up somewhere.



    I'm hoping there's a nasty bit-shifty way to produce a-z in less than 50 characters.



    I messed around with i=97;Array(26).map(x=>String.fromChar....i++



    but it was always way longer by the time I joined then split the array(26) to be useable






    Edit: I've gotten it down to



    [...Array(26)].reduce(a=>a+String.fromCharCode(i++),'',i=97)


    60 bytes


    @muddyfish, LuisMendo: This is on-topic per meta.

    `[...Array(26)].map((q,w)=>String.fromCharCode(w+97))` is 52 bytes and add another 7 for the `.join\`\``

    @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?

    @CharlieWynn `[...Array(26)].map((q,w)=>String.fromCharCode(w+97)).join\`\``

    @dev-null join`` is new to me!

  • edc65

    edc65 Correct answer

    5 years ago

    Alternative to String.fromCharCode


    ... if you are happy with a lowercase only alphabet.



    for(i=9,a='';++i<36;)a+=i.toString(36) // 38, cannot be used in an expression
    [...Array(26)].map(_=>(++i).toString(36),i=9).join`` // 52 directly returnig the string desired
    [...Array(26)].map(_=>a+=(++i).toString(36),a='',i=9) // 53 assign to a variable
    (i=9,[for(_ of Array(26))(++i).toString(36)].join``) // 52 ES7 direct return
    i=9,a='',[for(_ of Array(26))a+=(++i).toString(36)] // 51 ES7 assign to a variable

    Oh dang, that is clever. So it's starting with 10, converting to base 36 and printing it? so a-z!

    Are those a='' and i=9 arguments of map function call? Checked Array.prototype.map() on mdn and it doesn't look like map support such arguments..

    @JaySomedon those are argument for map function call, in a way, Javascript functions tipically don't care and discard parameters they do not expect. So I initialize a variable I need, while adding a parameter that is of no use for the called function

    @JaySomedon see also this answer and related comments https://codegolf.stackexchange.com/a/2684/21348

    @edc65 aha I see! That's neat! So here, when javascript evaluates arguments like i=9 in map(), it actually creates a global variable i then assigns 9 to that?

    @JaySomedon correct

    @edc65 One more thing, are these same: `var a = [...Array(26)]`, `var b = Array(26)`, `var c = new Array(26)`? In console, `a` prints `[undefined, undefined..]` and b and c print `[empty slots...]` but all `a[0]``b[0]``c[0]` evaluate to `undefined`. But only calling `map` on `[...Array(26)]` gives me alphabets, calling on the other two give me nothing, wonder why. Also on mdn, I can't find any example about `Array(n)`, so I wonder how `Array(n)` and `new Array(n)` are related with each other?

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM