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: 909.322 // 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;} |
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.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>