干得好:
var substrSize;
while (str.length) {
substrSize = Math.floor(Math.random()*3)+1; // at most 4?
if (substrSize >= str.length)
randomChar = str;
else
randomChar = str.substr(0,substrSize);
str = str.substr(randomChar.length);
console.log(randomChar);
}
或者:
var j = 0;
while (j < str.length) {
var n= j+Math.floor(Math.random() * 3) + 1;
if (n> str.length) n= str.length;
console.log(str.substring(j, n));
j = n;
}
或者:
var j = 0;
while (j < str.length) {
var n= Math.floor(Math.random() * 3) + 1;
if (j+n> str.length) n= str.length-j;
console.log(str.substr(j, n));
j += n;
}