You are viewing a single comment's thread. Return to all comments →
Javascript:
function caesarCipher(s, k) { let finalString = ''; const regex = /^[a-zA-Z]$/; for (const char of s) { let shift = char.charCodeAt(0)+k; if (shift > 90 && char.charCodeAt(0) < 91) { const newPosition = ((k - 90%char.charCodeAt(0))%26) || 26; shift = 64 + newPosition; } if (shift > 122 && char.charCodeAt(0) < 123) { const newPosition = ((k - 122%char.charCodeAt(0))%26) || 26; shift = 96 + newPosition; } if (regex.test(char)) { finalString += String.fromCharCode(shift); } else { finalString += char; } } return finalString; };
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 →
Javascript: