function dyslectize(text, wordTrehslod, letterTreshhold) { wordTrehslod = wordTrehslod || 0.3; letterTreshhold = letterTreshhold || 0.5; var words = text.split(" "); function isVowel(letter) { var vowels = "aeiouy"; return vowels.indexOf(letter.toLowerCase()) > -1; } function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } words.forEach(function(word, index){ if (Math.random() > wordTrehslod) { var letters = word.split(""); var firstUpperCaseLetter = letters[0] === letters[0].toUpperCase(); var swappable = []; letters.forEach(function(letter, index){ if (letter.match(/[a-z]/i) && !isVowel(letter) && Math.random() > letterTreshhold ) { swappable.push(index); } }); var newSwappable = shuffleArray(swappable.concat()); var ll = letters.concat(); newSwappable.forEach(function(item, index){ letters.splice(swappable[index], 1, ll[item]); }); words[index] = letters.join("").toLowerCase(); if (firstUpperCaseLetter) { words[index] = words[index].substr(0, 1).toUpperCase() + words[index].substr(1); } } }); return words.join(" "); }