Use PHP functions in JavaScript

JavaScript str_shuffle

Shuffles string. One permutation of all possible is created

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
function str_shuffle (str) {
    // Shuffles string. One permutation of all possible is created  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/str_shuffle    // +   original by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: shuffled = str_shuffle("abcdef");
    // *     results 1: shuffled.length == 6
    
    if (str == undefined) {        throw 'Wrong parameter count for str_shuffle()';
    }
    
    var getRandomInt = function (max) {
        return Math.floor(Math.random() * (max + 1));    };
    var newStr = '', rand = 0;
    
    while (str.length) {
        rand = getRandomInt(str.length-1);        newStr += str.charAt(rand);
        str = str.substring(0, rand)+str.substr(rand+1);
    }
    
    return newStr;}
external links: original PHP docs | raw js source

Examples

Running

1
shuffled = str_shuffle("abcdef");

Should result in

1
shuffled.length == 6

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have str_shuffle goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Hans B PUFAL
Jun 20th Permalink

q  On reviewing the code I note an error
the line

newStr.push (str.splice (Math.floor (Math.random () * (str.length - 1)) , 1)[0]);


should read

newStr.push (str.splice (Math.floor (Math.random () * (str.length)) , 1)[0]);


Gravatar
Hans B PUFAL
Jun 20th Permalink

q  

function str_shuffle (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: shuffled = str_shuffle("abcdef");
    // *     results 1: shuffled.length == 6

    var newStr = [];
    
    if (arguments.length < 1) {
        throw 'str_shuffle : Parameter str not specified';
    }
        
    if (typeof str !== 'string') {
        throw 'str_shuffle : Parameter str ( = ' + str + ') is not a string';
    }
    
    str = str.split (''); 
    while (str.length) {
        newStr.push (str.splice (Math.floor (Math.random () * (str.length - 1)) , 1)[0]);
    }
    
    return newStr.join ('');
}



In my version (above) I check the argument type explicitly (simply checking for undefined does not ensure that a parameter was passed nor that it is a string). I also provide the actual parameter value in the exception since this usually makes debugging much easier.

Perhaps php.js could adopt a standard in this area.

I use array splice to extract the random character from str (which is split into a character array) which returns the extracted character to be added to newStr. This means that the rand variable is no longer needed. Putting getRandInt inline saves time and characters.

Finally newStr is built in an array and converted at the return since (for long strings) this is more efficient than the str += char construct.


Contribute a New function