Tell us what’s happening:
Hey,
This is the way I came up with for solving the Caesar’s cipher problem in the JavaScript Algorithms and Data Structures Projects.
I’d like some feedback about my solution - what are the drawbacks/advantages of this approach?
Your code so far
function rot13(str) { // LBH QVQ VG!
let strArray = str.split("");
let decoder = ["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"];
let coder = ["N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"];
for(let i=0; i<strArray.length; i++){
if(coder.indexOf(strArray[i])!==-1){
strArray[i]=decoder[coder.indexOf(strArray[i])];
console.log(strArray);
}
}
return strArray.join("");
}
// Change the inputs below to test
console.log(rot13("SERR PBQR PNZC"));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/caesars-cipher