Word Scrambler
yup
by SHELDON PASCIAK
HTML
<div id="scramble-output"></div>
CSS
#scramble-output {
font-family: Arial;
font-size: 30px;
font-weight: bold;
color: slategray;
}
JavaScript
//private vars
var charCount = 0; //keep track of which char we're looking for
var delayTime = 1; //the delay time in ms for scramble speed
var word = "scramble mamble jamble" //the sentence to scramble
var uiElem = $('#scramble-output');
this.resetAll = function() { //in case we want to add reset functionality in future
charCount = 0;
itemCount = 0;
};
scrambleLetters(word, uiElem, true);
function scrambleLetters(word, uiElem, nextChar) {
var thisCount = charCount;
var charToFind, randChar, asciiCode, oldStr, newStr;
if(charCount >= word.length) { //if we've gone through all the letters, then stop scrambling
clearTimeout(timerID);
charCount = 0;
return;
} else {
charToFind = word[charCount];
asciiCode = charToFind.charCodeAt(0); //get ascii
if(asciiCode !== 32) { //is 'space'(ascii 32)
randChar = letterRandomizer();
if(nextChar || uiElem.text().length < 1) {
uiElem.html( function() {
return String(this.innerHTML + randChar);
});
} else {
oldStr = uiElem.text();
newStr = oldStr.substring(0, oldStr.length-1);
uiElem.text(newStr + randChar);
}
if(randChar===charToFind) {
charCount++;
}
} else {
uiElem.html(uiElem.text()+" ");
if(uiElem.html().length > 18){
uiElem.html(uiElem.text() + '<br>');
}
charCount++;
}
}
//set a timer that calls the scrambleLetters function recursively
timerID = setTimeout(function() {
var nextChar = (thisCount < charCount)? true: false;
scrambleLetters(word, uiElem, nextChar);
}, delayTime);
}
function letterRandomizer() {
var randChar =...