Last active
December 19, 2015 04:29
-
-
Save oieioi/5897875 to your computer and use it in GitHub Desktop.
String shuffle and random and
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 文字列シャッフル | |
| String.prototype.shuffle = function() { | |
| var result = []; | |
| var ary = this.split(""); | |
| var l = ary.length; | |
| for (var i = 0;i < l;i++) { | |
| var random = Math.floor(Math.random()*ary.length); | |
| result.push(ary[random]); | |
| ary.splice(random,1); | |
| } | |
| return result.join(""); | |
| }; | |
| // 文字列をcount数分ランダムで返す | |
| String.prototype.random = function(count) { | |
| var result = []; | |
| var ary = this.split(""); | |
| var l = count || ary.length; | |
| for (var i = 0;i < l;i++) { | |
| var random = Math.floor(Math.random()*ary.length); | |
| result.push(ary[random]); | |
| } | |
| return result.join(""); | |
| }; | |
| // 文字列中に含まれる各文字の数をカウントする | |
| String.prototype.charCount =function() { | |
| var l = this.length; | |
| var result = {}; | |
| for (var i = 0;i<l ;i++) { | |
| if (result[this[i]] == undefined) { | |
| result[this[i]] = 0; | |
| } | |
| result[this[i]] = result[this[i]] + 1; | |
| } | |
| return result; | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment