We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
constletter=26;constencrypt_char=(char:string,k:number):string=>{k=k%letter// ASCII codesletct_min,ct_max=0if(/([a-z])/.test(char))ct_max=(ct_min=97)+letter-1;if(/([A-Z])/.test(char))ct_max=(ct_min=65)+letter-1;// Only encrypt letter (mean a-z or A-Z)if(!ct_min||!ct_max)returnchar;letcode=char.charCodeAt(0)letcode_encrypted=code+kif(code_encrypted>ct_max)code_encrypted-=letterreturnString.fromCharCode(code_encrypted)}functioncaesarCipher(s:string,k:number):string{// doin encrypt every character in stringreturns.split('').map(c=>encrypt_char(c,k)).join('')}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Caesar Cipher
You are viewing a single comment's thread. Return to all comments →
My answer in Typescript