• Discussion
  • [CODE GOLF] Challenge #4 - Number to English

Ravage was asking cuz letti didn't have it in his anywho here's mine

// minified 455 version: all
function a(e){for(b=",one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thir,,fif,,,eigh,,,,twen,,for".split(","),o="",e+="",i=t=e.length;i>0;i-=3)p=e[i-3],q=e[i-2],r=e[i-1],o=(p>0?b[p]+" hundred"+(q>0||r>0?" ":""):"")+(q>0?(v=q<2?q+r|0:20+1*q,(b[v]||b[v-10]||b[v-20]||"")+(v>19?"ty":v>12?"teen":"")):"")+(1!=q&&r>0?(q>1?"-":p>0?"and ":"")+b[r]:"")+(e.substring(i-3,3)>0?", thousand , million , billion ".split(",")[(t-i)/3|0]:"")+o;return o}

since i don't think anyone else wants to take a crack at it

    15 days later

    Varrience
    399 chars
    (under 400 lets go!)

    a=",one,two,three,four,five,six,seven,eight,nine,ten,twen,thir,for,fif,six,seven,eigh,nine,,thousand,million,billion,ten,eleven,twelve,,fourteen".split(",");function c(e){for(d=e.toString().split("").map(Number),r="",j=v=d.length;j>0;j-=3)u=d[j-1],o=d[j-2],r=((p=d[j-3])?a[p]+" hundred ":"")+(o>1?a[o+9]+"ty"+(u?"-":""):"")+(1==o?a[u+23]||a[u+9]+"teen":a[u])+(u+o?" ":"")+a[(v-j)/3+19]+" "+r;return r}

    379 chars

    a=",one,two,three,four,five,six,seven,eight,nine,ten,twen,thir,for,fif,six,seven,eigh,nine,,thousand,million,billion,ten,eleven,twelve,,fourteen".split(","),c=n=>{for(r=e="",j=v=(d=(n+e).split(e)).length;j>0;)u=d[--j],o=d[--j],r=((p=d[--j])-0?a[p]+" hundred ":e)+(o>1?a[o- -9]+"ty"+(u?"-":e):e)+(1==o?a[u-0+23]||a[u-0+9]+"teen":a[u])+(u+o==0?e:" ")+a[(v-j)/3+18]+" "+r;return r};

    You never mentioned having to do it in code.org.

    Chat