Keyword Cipher | Function generates the encoded text ; This array represents the 26 letters of alphabets ; This loop inserts the keyword at the start of the encoded string ; To check whether the character is inserted earlier in the encoded string or not ; This loop inserts the remaining characters in the encoded string . ; Function that generates encodes ( cipher ) the message ; This loop ciphered the message . Spaces , special characters and numbers remain same . ; Hold the Keyword ; Function call to generate encoded text ; Message that need to encode ; Function call to print ciphered text
function encoder ( key ) { let encoded = " " ; let arr = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { arr [ i ] = false ; } for ( let i = 0 ; i < key . length ; i ++ ) { if ( key [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && key [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) { if ( arr [ key [ i ] . charCodeAt ( 0 ) - 65 ] == false ) { encoded += ( key [ i ] ) ; arr [ key [ i ] . charCodeAt ( 0 ) - 65 ] = true ; } } else if ( key [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && key [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) { if ( arr [ key [ i ] . charCodeAt ( 0 ) - 97 ] == false ) { encoded += String . fromCharCode ( key [ i ] . charCodeAt ( 0 ) - 32 ) ; arr [ key [ i ] . charCodeAt ( 0 ) - 97 ] = true ; } } } for ( let i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] == false ) { arr [ i ] = true ; encoded += String . fromCharCode ( i + 65 ) ; } } return encoded ; } function cipheredIt ( msg , encoded ) { let cipher = " " ; for ( let i = 0 ; i < msg . length ; i ++ ) { if ( msg [ i ] >= ' ' && msg [ i ] <= ' ' ) { let pos = msg [ i ] . charCodeAt ( 0 ) - 97 ; cipher += encoded [ pos ] ; } else if ( msg [ i ] >= ' ' && msg [ i ] <= ' ' ) { let pos = msg [ i ] . charCodeAt ( 0 ) - 65 ; cipher += encoded [ pos ] ; } else { cipher += msg [ i ] ; } } return cipher ; } let key ; key = " " ; document . write ( " " + key + " " ) ; let encoded = encoder ( key . split ( " " ) ) ; let message = " " ; document . write ( " " + message + " " ) ; document . write ( " " + cipheredIt ( message , encoded ) ) ;