<textarea id="input" class="form-control" placeholder="Enter some text, please">Tyger Tyger, burning bright,
In the forests of the night;
What immortal hand or eye,
Could frame thy fearful symmetry?
In what distant deeps or skies.
Burnt the fire of thine eyes?
On what wings dare he aspire?
What the hand, dare seize the fire?
And what shoulder, & what art,
Could twist the sinews of thy heart?
And when thy heart began to beat,
What dread hand? & what dread feet?
What the hammer? what the chain,
In what furnace was thy brain?
What the anvil? what dread grasp,
Dare its deadly terrors clasp!
When the stars threw down their spears
And water'd heaven with their tears:
Did he smile his work to see?
Did he who made the Lamb make thee?
Tyger Tyger burning bright,
In the forests of the night:
What immortal hand or eye,
Dare frame thy fearful symmetry?</textarea>
<button id="scramble" class="btn btn-default">
Scramble vowels
</button>
var vowelScrambler = {
init: function () {
$('#scramble').click(function () {
var input = $('#input');
var originalText = input.val();
var scrambledText = vowelScrambler.scrambleString(originalText);
input.val(scrambledText);
});
},
/**
* Changes all of the vowels in the provided string
*
* @param string
* @returns string
*/
scrambleString: function (string) {
var scrambled = [];
for (var i = 0, length = string.length; i < length; i++) {
scrambled.push(this.scrambleLetter(string[i]));
}
return scrambled.join('');
},
/**
* Scrambles a single vowel (or returns the consonant given to it)
*
* @param letter
* @return string
*/
scrambleLetter: function (letter) {
var vowels = ['a', 'e', 'i', 'o', 'u', 'y'];
if (vowels.indexOf(letter.toLowerCase()) == -1) {
return letter;
}
for (var i = 0, length = vowels.length; i < length; i++) {
if (letter.toLowerCase() == vowels[i]) {
vowels.splice(i, 1);
}
}
// Lowercase
if (letter == letter.toLowerCase()) {
return vowels[Math.floor(Math.random() * vowels.length)];
}
// Uppercase
return vowels[Math.floor(Math.random() * vowels.length)].toUpperCase();
}
};