Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <h1>speakCrypt</h1>
  
<input id="int" type="number" tabindex="0" placeholder="Integer to encrypt"  />
<input id="salt" type="text" tabindex="0" placeholder="Salt" />
<input id="result" type="text" tabindex="0" disabled="disabled" />
  
</body>
</html>
 
$(function(){
  
  $('#int, #salt').on('input', function () {
    if ( $('#int').val() == '' ) return $('#result').val('');
    var res = speakCrypt($('#int').val(), $('#salt').val());
    $('#result').val(res);
  });
  
  $('#int').val(Math.ceil(Math.random() * 10000)).trigger('input');
  
});
function speakCrypt (int, salt, dict) {
  
  dict = dict || [
    'a,e,i,o,u,oi,ou,ai,au,on,in',
    'b,c,d,f,g,j,k,l,m,n,p,r,s,t,v,w,x,y,z,bl,ch,cl,cr,dr,fl,tr,br'
  ];
  
  if (!isFinite(int) || int < 0)
    throw new TypeError('First argument must be a positive finite number.');
  
  // Alternate vowel-first and consonant-first syllables
  // depending on whether the int is pair or not
  
  var mod = (int % 2 === 0) ? 0 : 1;
  
  // Generate all syllables
  
  var syllables = [];
  
  dict[ mod ].split(',').forEach(function (first) {
    dict[ (mod+1) % 2 ].split(',').forEach(function (second) {
      syllables.push(first + second);
    });
  });
  
  // If there's a salt, use it to consistently shuffle the syllables
  
  if (salt) {
    
    var charCode, j, i, v, p; salt += '';
    
    for (i = syllables.length - 1, v = 0, p = 0; i > 0; i--, v++) {
      v %= salt.length;
      p += charCode = salt.charAt(v).charCodeAt(0);
      j = (charCode + v + p) % i;
    
      syllables.splice(j, 0, syllables.splice(i, 1)[0]);
    
    }
    
  }
  
  // Find how many steps to look for
  
  var steps = Math.ceil(Math.log(int) / Math.log(syllables.length));
  if (steps < 2) steps = 1;
  
  // Deduce syllables from steps
  
  var result = '', steppedIndex;
  
  for (var i = steps; i > 0; i--) {
    steppedIndex = Math.floor(int / Math.pow(syllables.length, i - 1));
    result += syllables[ Math.floor((steppedIndex % syllables.length) / 2) ];
  }
  
  return result;
  
}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
christophemaroispro
0viewers