JavaScript/ceasar/site.js
//based on the given key; finds a new letter and returns a new string.
based on the given key; finds a new letter and returns a new string.
based on the given key ; finds a new letter and returns a new string .
function caesarEncrypt(plainStr, key) {
    var result = '';
    for (var i = 0, len = plainStr.length; i < len; i++) {
        var indexInAlphabet = alphabet.indexOf(plainStr[i]); 
        var alphabetRemainder = alphabet.length - (indexInAlphabet + 1); 
        if (parseInt(key) <= alphabetRemainder) {
            newIndex = indexInAlphabet + parseInt(key); 
            newLetter = alphabet[newIndex]; 
            result += newLetter;
        } else {
            if ((parseInt(key) + alphabetRemainder) > alphabet.length) {
                if ((parseInt(key) - alphabetRemainder) % alphabet.length < 0) {
                    newIndex = alphabet.length - (parseInt(key) - alphabetRemainder) - 1;
                    newLetter = alphabet[newIndex];
                    result += newLetter;
                } else {
                    newIndex = ((parseInt(key) - alphabetRemainder) % alphabet.length) - 1;
                    newLetter = alphabet[newIndex];
                    result += newLetter;
                }
            } else if ((parseInt(key) + alphabetRemainder) === alphabet.length) {
                newLetter = alphabet[alphabet.length - 1];
                result += newLetter;
            } else {
                newIndex = (parseInt(key) - alphabetRemainder) - 1;
                newLetter = alphabet[newIndex];
                result += newLetter;
            }
        }
        $('#encryptResult').html(result);
    }
}
function caesarEncrypt ( plainStr , key ) { var result = '' ; for ( var i = 0 , len = plainStr . length ; i < len ; i ++ ) { var indexInAlphabet = alphabet . indexOf ( plainStr [ i ] ) ; var alphabetRemainder = alphabet . length - ( indexInAlphabet + 1 ) ; if ( parseInt ( key ) <= alphabetRemainder ) { newIndex = indexInAlphabet + parseInt ( key ) ; newLetter = alphabet [ newIndex ] ; result += newLetter ; } else { if ( ( parseInt ( key ) + alphabetRemainder ) > alphabet . length ) { if ( ( parseInt ( key ) - alphabetRemainder ) % alphabet . length < 0 ) { newIndex = alphabet . length - ( parseInt ( key ) - alphabetRemainder ) - 1 ; newLetter = alphabet [ newIndex ] ; result += newLetter ; } else { newIndex = ( ( parseInt ( key ) - alphabetRemainder ) % alphabet . length ) - 1 ; newLetter = alphabet [ newIndex ] ; result += newLetter ; } } else if ( ( parseInt ( key ) + alphabetRemainder ) === alphabet . length ) { newLetter = alphabet [ alphabet . length - 1 ] ; result += newLetter ; } else { newIndex = ( parseInt ( key ) - alphabetRemainder ) - 1 ; newLetter = alphabet [ newIndex ] ; result += newLetter ; } } $ ( '#encryptResult' ) . html ( result ) ; } }
based on the given key; finds a new letter and returns a new string.
based on the given key ; finds a new letter and returns a new string .
// determining the index of a letter in the alphabet array // determining how many letters are left till the end of array // determining a new index // determining a new letter //goes though the alphabet array several times until the right index is found