sequence - Sequential strings javascript -
i'm stuck on sequential string generation, last iteration of code below, trying return ac
.
var charcode = 'abcdefghijklmnopqrstuvwxyz0123456789_'; var arr = []; var len = 1; var current = 0; for(var = 0; < 40; i++) { if(current == charcode.length) { current = 0 len++ } var tmpstr = '' for(var l = 0; l < len; l++) { tmpstr = tmpstr + charcode[current]; } console.log(tmpstr) current++ }
however, produces cc
.
sorry, somehow deleted intended output
a, b, c ... aa, ab, ac ... ba, bc, bb ... ∞
my current understanding suggests within l < len
loop should check if len > 1
, if break out , loop current charcode[x]
can't wrap head around breaking in , out grows.
you can this:
var charcode = 'abcdefghijklmnopqrstuvwxyz0123456789_'; var arr = []; var len = 0; var current = 0; for(var = 0; < 40; i++) { if(current == charcode.length) { current = 0; len++; } var tmpstr = (len===0) ? '' : charcode[len-1]; tmpstr = tmpstr + charcode[current]; current++; console.log(tmpstr); }
in brief, didn't need second loop, iteration instead (which achieved through charcode[len-1]
).
js bin here.
update
if need continuos loop until end of charset, can use this:
this 1 introduces third variable (digits
) iteration , choses .substr()
instead of single character out of charset, everytime end of charset reached, digit added. variable called x
sets limit. have tried 4000
, looks working.
var charcode = 'abcdefghijklmnopqrstuvwxyz0123456789_', arr = [], len = 0, digits = 2, current = 0, tmpstr = ''; var x = 4000; for(var i=0; i<x; i++) { if(current === charcode.length) { current = 0; len++; if(tmpstr.substr(0,1) === charcode.substr(-digits,1) || tmpstr === charcode.substr(-1) + '' + charcode.substr(-1)) { len = 1; digits++; } } if(digits === charcode.length) { console.log(charcode); console.log(i + ' results found'); break; } tmpstr = (len===0) ? '' : charcode.substr([len-1], digits-1); tmpstr += charcode[current]; current++; console.log(tmpstr); }
final js bin here.
Comments
Post a Comment