Suppose all the characters in JavaScript were a, b, c, d, e and f. What I'm trying to do is create a random mapping between characters. So the above might be like
{ `a` : `e`,
`b` : `b`,
`c` : `e`,
`d` : `b`,
`e` : `a`,
`f` : `c` }
First, how can I get all the possible characters in JavaScript?
var AllChars = new Array();
// ... fill AllChars with the full range of characters
Here is an example on how to generate an array with all the lowercase letters:
var AllChars = [];
for (var i=97; i<123; i++)
AllChars.push(String.fromCharCode(i));
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
Edit to include all ascii printable characters per Aaron recommendation:
var AllChars = [];
for (var i=32; i<127; i++)
AllChars.push(String.fromCharCode(i));
[" ", "!", """, "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With