node.js - How to add a common string in every randomly generated string Javascript -
i generating random strings using below function in node.js. wanted know if there way create text strings appropriately common string within every randomly generated string.
edit: common string can in location of generated string
for example:
randomly generated string - cxqtooxyy4
can add 'abc' or 'abc' within string - cxqtoabcoxyy4 or cxqtoabcoxyy4 respectively.
my code -
var randomtextarraygeneration = function(size) { var text = ""; var possible = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; for(var i=0;i<size;i++) text += possible.charat(math.floor(math.random() * possible.length)); return text; }
can tell me how do this? helpful.
var n = text.length; //the size of random string var randomposition = math.floor((math.random() * n) + 1); //generate random number between 1 , size of string //separate string in 2 strings var text1 = text.substring(1, randomposition); var text2 = text.substring(randomposition, n); //create final string adding common string between 2 halves var textfinal = text1 + commonstring + text2; return textfinal;
i don't remember how works .substring()
, may want change 1 0 in places.
Comments
Post a Comment