Trans

Switches corresponding letter pairs

by Marcus Baptiste

HTML

<textarea id="words"></textarea>
<br>
<button id="btncalc" onclick="calc();">Calculate</button>
<br>
<br>
<input type="checkbox" id="opt">Two way
<br>
    <p id="dispNewText"> New Text </p>

JavaScript

$(document).ready(function () {
    $("#btncalc").click(function () {
        var testtext = $(this).siblings("#words").val(),
            testchar = 0,
            newchar = "",
            newtext = "",
            twoway = false;

        if ($(this).siblings("#opt").is(":checked")) {
            twoway = true;
        }

        for (i = 0; i < testtext.length; i++) {
            testchar = testtext.charCodeAt(i);
            switch (testchar) {
                case 107:
                case 111:
                case 101:
                    newchar = testchar + 2;
                    break;
                    
                case 109:
                case 113:
                case 103:
                    if(twoway) { newchar = testchar - 2; }
                    else newchar = testchar;
                    break;
                
                default:
                    newchar = testchar;
            }
            
            newtext += String.fromCharCode(newchar);
        }
        
        $(this).siblings("#dispNewText").text(newtext);


    });
});