This code works and Iβve tried to explain my thinking, but I really wanted to use a regular expression to match all the Capital letters, but I struggled to get it to work.
function rot13(str) { // LBH QVQ VG!
var code = []; // create an empty array to hold the code numbers var newString = []; // create an empty array to hold the decoded string
//iterate through the string and push its //charCode to the code array for (var i = 0; i < str.length; i++) {
code.push(str.charCodeAt([i])); } //console.log(code);
// function to map each value of code to a new array // codeRot13, adding 13 to each number var codeRot13 = code.map(function (val) {
return val + 13; });
//console.log(codeRot13);
//Iterate though codeRote13 and if the value is above 90(z) return // to 65(a) and add the balance //else if that results in going over 78 (A+13) remove the 13. for (var j = 0; j < codeRot13.length; j++) {
if (codeRot13[j] > 90) { codeRot13[j] = 65 + (codeRot13[j] - 91); } else if (codeRot13[j] < 78) {
codeRot13[j] = codeRot13[j] - 13; } }
//console.log(codeRot13);
//Turn it back to a string for (var k = 0; k < codeRot13.length; k++) {
newString.push(String.fromCharCode(codeRot13[k]));
}
newString = newString.join('');
//console.log(newString);
return newString;
}
Any help on how I could do this and still understand what going on would be appreciated. Thanks