We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4b67bca commit f9ac7b6Copy full SHA for f9ac7b6
functions/strings/str_shuffle.js
@@ -3,18 +3,23 @@ function str_shuffle (str) {
3
// + original by: Brett Zamir (http://brett-zamir.me)
4
// * example 1: shuffled = str_shuffle("abcdef");
5
// * results 1: shuffled.length == 6
6
- if (str == null) {
+ if (arguments.length === 0) {
7
throw 'Wrong parameter count for str_shuffle()';
8
}
9
10
+ if (str == null) {
11
+ return '';
12
+ }
13
+
14
str += '';
15
- var newStr = '', rand;
16
+ var newStr = '', rand, i = str.length;
17
- while (str.length) {
- rand = Math.floor(Math.random() * str.length);
18
+ while (i) {
19
+ rand = Math.floor(Math.random() * i);
20
newStr += str.charAt(rand);
21
str = str.substring(0, rand) + str.substr(rand + 1);
22
+ i--;
23
24
25
return newStr;
0 commit comments