Skip to content

Commit 4b67bca

Browse files
committed
str_shuffle should return empty string for null argument. No need to define getRandomInt function for such small calculation
1 parent 47d821c commit 4b67bca

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

functions/strings/str_shuffle.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ function str_shuffle (str) {
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// * example 1: shuffled = str_shuffle("abcdef");
55
// * results 1: shuffled.length == 6
6-
if (str == undefined) {
6+
if (str == null) {
77
throw 'Wrong parameter count for str_shuffle()';
88
}
9+
10+
str += '';
911

10-
var getRandomInt = function (max) {
11-
return Math.floor(Math.random() * (max + 1));
12-
};
13-
var newStr = '',
14-
rand = 0;
12+
var newStr = '', rand;
1513

1614
while (str.length) {
17-
rand = getRandomInt(str.length - 1);
15+
rand = Math.floor(Math.random() * str.length);
1816
newStr += str.charAt(rand);
1917
str = str.substring(0, rand) + str.substr(rand + 1);
2018
}

0 commit comments

Comments
 (0)