admin管理员组文章数量:1355599
I am trying to shuffle words. I Want to shuffle First with its respective alphabets. Currently it is shuffling First with Second alphabets..
I Want to split words & shuffle "sFtir Seocdn".
String.prototype.shuffle = function () {
var a = this.split(""),
n = a.length;
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("");
}
alert("First Second".shuffle());
I tried splitting by below code, but then it only splits & shuffles words not letters.
var a = this.split(" "),
return a.join(" ");
Jsfiddle link : / Pls suggest what should I do.
I am trying to shuffle words. I Want to shuffle First with its respective alphabets. Currently it is shuffling First with Second alphabets..
I Want to split words & shuffle "sFtir Seocdn".
String.prototype.shuffle = function () {
var a = this.split(""),
n = a.length;
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("");
}
alert("First Second".shuffle());
I tried splitting by below code, but then it only splits & shuffles words not letters.
var a = this.split(" "),
return a.join(" ");
Jsfiddle link : http://jsfiddle/9L8rs/1/ Pls suggest what should I do.
Share Improve this question asked Jul 1, 2014 at 14:22 itsMeitsMe 6333 gold badges10 silver badges24 bronze badges 3- You need to call shuffle on the individual words... – Ian Commented Jul 1, 2014 at 14:25
-
2
First split with a space, then loop through those results, and split with
string.split('')
. Then do your shuffle and join – bottens Commented Jul 1, 2014 at 14:25 -
1
In addition to VisioN's answer, here's a solution keeping everything inside of
shuffle
(so the code usingshuffle
doesn't have to worry about words): jsfiddle/7hjg4 . But I guess it depends on how you're expecting to use this – Ian Commented Jul 1, 2014 at 14:30
3 Answers
Reset to default 6Just split the string into words and shuffle the letters in words separately:
"First Second".split(' ').map(function(w) { return w.shuffle(); }).join(' ');
Here .split
and .map
(polyfill may be applied for old browsers) methods can help.
DEMO: http://jsfiddle/9L8rs/2/
Try splitting the ining String first:
String.prototype.shuffle = function() {
return this.split(" ").map(function(word, i) {
var a = word.split(""),
n = a.length;
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("");
}).join(" ");
}
alert("First Second".shuffle());
Updated JSFiddle
I just added a function shuffleWords that split on words and call your shuffle function on each of them. ex. a[i] = a[i].shuffle();
String.prototype.shuffleWords = function () {
var a = this.split(" "),
n = a.length;
for (var i = 0; i < n; i++) {
a[i] = a[i].shuffle();
}
return a.join(" ");
}
See your updated jsfiddle
本文标签: javascriptSplit words amp shufflejumble lettersStack Overflow
版权声明:本文标题:javascript - Split words & shufflejumble letters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744043081a2581024.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论