Hello guys,
Here’s my not so elegant solution
(almost no brain cells were harmed in the making of this solution)
Solution #1
const CIPHER = {
A:'N', B:'O', C:'P', D:'Q', E:'R', F:'S', G:'T', H:'U', I: 'V',
J:'W', K:'X', L:'Y', M:'Z', N:'A', O:'B', P:'C', Q:'D', R:'E',
S:'F', T:'G', U:'H', V:'I', W:'J', X:'K', Y:'L', Z:'M'
};
function rot13(str) {
let decoded = "";
for(let i = 0; i < str.length; i++){
if(CIPHER.hasOwnProperty(str[i]) === true){
decoded += (CIPHER[str[i]]);
}
else if(CIPHER.hasOwnProperty(str[i]) === false){
decoded += (str[i]);
}
}
return decoded;
}
console.log(rot13("SERR. PBQR. PNZC?!"));
(Edit: Brushed up my original solution a bit after watching the video )
After watching, I realized that my solution is not dynamic/flexible as it only works exclusively for ROT13. So I tried doing the project again. This time I can set the amount of shifting or rotation from 1 to 13.
(RIP brain cells)
Solution #2
const ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // alphabet - 26 letters
function rot13(str){
let decoded = ""; // storage for decoded string
let newLetter = ""; // storage for the new/decoded letter based from its new index
let newIndex; // storage for the new index based on the number of shifts
let shift = 13; // the number of shift (applicable only from 1-13)
let maxShift = shift*2; // maximum index value a new/decoded letter can have
for(let i = 0; i < str.length; i++){ // for loop to access every letter in the given string
if(ALPHA.includes(str[i]) === true){ // if a letter in the given string is present at ALPHA (line 2)
newIndex = ALPHA.indexOf(str[i]) + shift; // then the letter should move *shift* places
if(newIndex < maxShift){ // if the new index of the new letter (line 13) is < maxShift (line 9)
newLetter = ALPHA[newIndex]; // then store the new letter in the variable newLetter
decoded += newLetter; // and then store newLetter to decoded variable
}
else if(newIndex >= maxShift){ // but if new index of the new letter (line 13) is >= maxShift (line 9)
newIndex -= maxShift; // then subtract the new index of the new letter by maxShift (line 9)
newLetter = ALPHA[newIndex]; // and then store the new/decoded letter based on its new index in the variable newLetter
decoded += newLetter; // and then store newLetter to decoded variable
}
}
else if(ALPHA.includes(str[i]) === false){ // if a letter in the given string is not present at ALPHA (line 2)
decoded += str[i]; // then just store it to decoded variable
}
}
return decoded; // after looping, return the value of decoded variable out of the function
};
console.log(rot13("SERR. PBQR. PNZC?!"));
Since I have the luxury of time and wanted to be extra, I tried to enable the number of rotations from 14 to 26 too like the one on this website
I realized after doing this I could have used charCodeAt() But whatever. I like beating myself up with math problems anyway
(yep, I just love math)
Solution #3
const ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function rot13(str){
let upperStr = str.toUpperCase();
let decoded = "";
let newLetter = "";
let newIndex;
let rot = 13;
let maxIndex = 25 - rot;
for(let i = 0; i < upperStr.length; i++){
if(ALPHA.includes(upperStr[i]) === true){
if(ALPHA.indexOf(upperStr[i]) <= maxIndex){
newIndex = ALPHA.indexOf(upperStr[i]) + rot;
newLetter = ALPHA[newIndex];
decoded += newLetter;
}
else if(ALPHA.indexOf(upperStr[i]) > maxIndex){
newIndex = ALPHA.indexOf(upperStr[i]) - maxIndex - 1;
newLetter = ALPHA[newIndex];
decoded += newLetter;
}
}
else if(ALPHA.includes(upperStr[i]) === false){
decoded += upperStr[i];
}
}
return decoded;
};
console.log(rot13("abcdefghijklmnopqrstuvwxyz !?"))