JSFiddle - React, Tailwind, and code Playground
JavaScript
function CC(str, num) {
var e = "";
var al = [];
for (var j = 0; j <= 25; j++) {
al.push(String.fromCharCode(j + 97));
}
for (var i = 0; i < str.length; i++) {
var pos = al.indexOf(str[i].toLowerCase());
if (pos > -1) {
pos += num;
if (pos > 25) {
pos = pos - 26;
}
e += ((str[i]).toUpperCase() === str[i]) ? (al[pos]).toUpperCase() : al[pos];
} else {
e += str[i];
}
}
return e;
}
console.log(CC("Fcjjm Yqfjcw!", 2));
console.log(CC("Nkrru Gynrke, gmgot!", 20));
console.log(CC("Nkrru toi!", 20));