portfolio/myportfolio/portfoliopages/static/portfoliopages/js/caesar.js
//an opposite process with a similar logic as above
an opposite process with a similar logic as above
an opposite process with a similar logic as above
function caesarDecrypt(plainStr, key) {
    var result = '';
    for (var i = 0, len = plainStr.length; i < len; i++) {
        var indexInAlphabet = alphabet.indexOf(plainStr[i]); 
        var alphabetRemainder = indexInAlphabet; 
        if (parseInt(key) <= alphabetRemainder) {
            newIndex = indexInAlphabet - parseInt(key); 
            newLetter = alphabet[newIndex]; 
            result += newLetter;
        } else {
            if ((parseInt(key) + alphabetRemainder) > alphabet.length) {
                if ((parseInt(key) - (alphabet.length - alphabetRemainder)) === alphabet.length) {
                newLetter = alphabet[alphabet.length - 2];
                result += newLetter;
                } else {
                    newIndex = indexInAlphabet + (indexInAlphabet - ((parseInt(key) + alphabetRemainder) % alphabet.length));
                    console.log(newIndex);
                    if (newIndex < 0) {
                        newLetter = alphabet[newIndex + alphabet.length];
                        result += newLetter;
                    } else {
                        newLetter = alphabet[newIndex];
                        result += newLetter;
                    }
                }
            } else if ((parseInt(key) + alphabetRemainder) === alphabet.length) {
                newLetter = alphabet[alphabet.length - alphabet.length];
                result += newLetter;
            } else {
                newIndex = alphabet.length - parseInt(key) + alphabetRemainder;
                newLetter = alphabet[newIndex];
                result += newLetter;
            }
        }
        $('#decryptResult').html(result).css('background-color', '#5b5a5a');
    }
}
function caesarDecrypt ( plainStr , key ) { var result = '' ; for ( var i = 0 , len = plainStr . length ; i < len ; i ++ ) { var indexInAlphabet = alphabet . indexOf ( plainStr [ i ] ) ; var alphabetRemainder = indexInAlphabet ; if ( parseInt ( key ) <= alphabetRemainder ) { newIndex = indexInAlphabet - parseInt ( key ) ; newLetter = alphabet [ newIndex ] ; result += newLetter ; } else { if ( ( parseInt ( key ) + alphabetRemainder ) > alphabet . length ) { if ( ( parseInt ( key ) - ( alphabet . length - alphabetRemainder ) ) === alphabet . length ) { newLetter = alphabet [ alphabet . length - 2 ] ; result += newLetter ; } else { newIndex = indexInAlphabet + ( indexInAlphabet - ( ( parseInt ( key ) + alphabetRemainder ) % alphabet . length ) ) ; console . log ( newIndex ) ; if ( newIndex < 0 ) { newLetter = alphabet [ newIndex + alphabet . length ] ; result += newLetter ; } else { newLetter = alphabet [ newIndex ] ; result += newLetter ; } } } else if ( ( parseInt ( key ) + alphabetRemainder ) === alphabet . length ) { newLetter = alphabet [ alphabet . length - alphabet . length ] ; result += newLetter ; } else { newIndex = alphabet . length - parseInt ( key ) + alphabetRemainder ; newLetter = alphabet [ newIndex ] ; result += newLetter ; } } $ ( '#decryptResult' ) . html ( result ) . css ( 'background-color' , '#5b5a5a' ) ; } }
an opposite process with a similar logic as above
an opposite process with a similar logic as above
// determining the index of a letter in the alphabet array // determining how many letters are left till the beginning of array // determining a new index // determining a new letter //goes though the alphabet array several times until the right index is found