Hello everyone,
I was thinking about this project and within that thinking came up with this solution. I hope it’s ok to posting it day before we will work on it. It’s done with nested for loops and arrays but I have a question about non-alphabetic characters (it’s in if conditions at the bottom of the code). Non-alphabetic characters are very much hard coded and it is done just for passing FCC, but would be really grateful if someone can suggest solution for this part, with regex or something similar. I don’t understand what FCC means by saying “Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.” What they mean by “passing” and what by “do not transform”?
I understand that this solution is also hard coded for alphabet, but it’s just my thinking as I mentioned…Looking forward for tomorrow. Anyway, would also like to hear how someone feels about this solution. Thank you in advance, great Bootcamp and amazing lecturer and guests!
function rot13(str) {
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var arrString = str.split("");
console.log(arrString);
var arr3 = [];
for (var i = 0; i < arrString.length; i++) {
for(var j = 0; j < alphabet.length; j++) {
if(alphabet.indexOf(alphabet[j]) <= 12) {
if(arrString[i] == alphabet[j]) {
arr3.push(alphabet[j + 13]);
console.log(arr3);
}
} else {
if(arrString[i] == alphabet[j]) {
arr3.push(alphabet[j - 13]);
console.log(arr3);
}
}
}
if(arrString[i] == " ") {
arr3.push(" ");
} else if (arrString[i] == "!") {
arr3.push("!");
} else if (arrString[i] == "?") {
arr3.push("?");
} else if(arrString[i] == ".") {
arr3.push(".");
}
}
str = arr3.join("");
console.log(str);
return str;
}
//console.log(rot13("SERR PBQR PNZC"));
rot13("SERR PBQR PNZC!");