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.
function caesarCipher(s, k) {
let alpbt = 'abcdefghijklmnopqrstuvwxyz';
let capsAlpbt = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let result = '';
k = k % 26;
function add(arr, index) {
let newIndex = (index + k) % arr.length;
result += arr[newIndex];
}
for (let i = 0; i < s.length; i++) {
let index;
if (alpbt.includes(s[i])) {
index = alpbt.indexOf(s[i]);
add(alpbt, index)
} else if (capsAlpbt.includes(s[i])) {
index = capsAlpbt.indexOf(s[i]);
add(capsAlpbt, index)
} else {
result += s[i]
}
}
return result;}
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 →
javascript solution :