I need to animate the function descramble()
. So, it changes the first character then moves on the next character and so on. I have tried using setTimeout()
but that just delays the launch of the function. I just need to animate the ROT13 conversion letter by letter.
I'm using slice()
to remove the first element of the string replacing it with ROT13 then replacing the string with the new letter plus the whole string. I was unable to found a way to only remove the first letter of the paragraph. You can click the third paragraph in the jsfiddle to see the conversion.
$("#last-second-inside p:nth-child(3)").one('click', function() {
$(this).css('cursor', 'default');
var str = "Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.";
var strArr = str.split('');
var decodedArr = [];
var increaseNum = -1;
descramble();
function descramble() {
strArr.map(function(num) {
increaseNum++;
var currentLetter = num.charCodeAt();
if (currentLetter >= 65 && currentLetter <= 90 || currentLetter >= 97 && currentLetter <= 122) {
if (currentLetter >= 78 && currentLetter <= 90 || currentLetter >= 110 && currentLetter >= 97) {
decodedArr.push(String.fromCharCode(currentLetter - 13));
} else {
decodedArr.push(String.fromCharCode(currentLetter + 13));
}
} else {
decodedArr.push(num);
}
var sliced = str.slice(increaseNum + 1, str.length - 1);
$("#last-second-inside p:nth-child(3)").text(decodedArr.join('') + sliced);
})
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="last-second-inside">
<p>You probably now understand that how binary works and how simple it is.</p>
<p>Now, I need you to think about that how you might display 8 in binary. After giving it a shot, you can click on the next paragraph to descramble it.</p>
<p>Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.</p>
</div>