javascript - JS: Array shuffling, not string shuffling -


i trying shuffle string array using js. hard-coded 3 sets of characters. 3 characters each set chosen @ random , concatenated. concatenated string 3 letters, followed 3 numbers, followed 3 symbols. want shuffle concatenated string order randomized.

i checked how randomize (shuffle) javascript array?, , algorithm , code matches second solution was, except fact have shuffle loop in bigger function (instead of own function). perhaps should add shuffling own function , call within bigger function, think still give me same result.

the "shuffling part" of function isn't shuffling. when debug console.log(temp) , console.log(tp[rnd]), correct values showing. basically, function returning unshuffled string wanted shuffle.

var letterset = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; var numberset = "0123456789"; var symbolset = "~!@#$%^&*()-_+=><";  this.generatepassword = function() {         var rl = "";         var rn = "";         var rs = "";         var tp = "";          // 3 random letters         for(var = 0; < 3; i++) {             var rnd = math.floor((math.random() * 52));             rl += letterset[rnd];         }          // 3 random numbers         for(var = 0; < 3; i++) {             var rnd = math.floor((math.random() * 10));             rn += numberset[rnd];         }          // 3 random symbols         for(var = 0; < 3; i++) {             var rnd = math.floor((math.random() * 17));             rs += symbolset[rnd];         }          // string concatenation         tp = rl + rn + rs;          // shuffling part         for(var = 0; < tp.length; i++) {             var rnd = math.floor(math.random() * tp.length);             var temp = tp[i];             tp[i] = tp[rnd];             tp[rnd] = temp;         }          return tp;     }(); 

i don't understand going wrong.

your shuffle bit off, looked @ this uses array shuffle.

// string concatenation tp = rl + rn + rs; tp=tp.split('');  // shuffling part for(var = 0; < tp.length; i++) {    var rnd = math.floor(math.random() * tp.length);    var temp = tp[i];    tp[i] = tp[rnd];    tp[rnd] = temp; } tp=tp.join(""); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -