Increment a string with letters?

前端 未结 12 1280
礼貌的吻别
礼貌的吻别 2021-02-08 12:35

I need to increment a string from.. let\'s say aaa to zzz and write every incrementation in the console (is incrementation even a word?). It would go s

12条回答
  •  我在风中等你
    2021-02-08 13:05

    Treat the string like it's a base 36 number.

    Convert it to decimal, add 1, convert back to base 36, and replace any zeroes with the letter 'a':

    var str= 'aaa',
        s= str;
    
    while(str!=='zzz') {
      str= ((parseInt(str, 36)+1).toString(36)).replace(/0/g,'a');
      s+= ' '+str;
    }
    
    document.body.innerHTML= s;

提交回复
热议问题