Feedback on 2 array implementation of Caesars Cipher

Tell us what’s happening:

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++){

  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:

First go head and blur your solution with spoiler tag. It’s not a good idea to give a way solutions easily.

IMHO, pro is the code is easily readable. Con is performance. You will find out that nested forloops are not optimal. indexOf uses forloop under the hood.

With that said another way you can solve this exercise is using ascii code. Each character can be represented by a number, so by converting them to ascii codes and compare them with numbers in conditionals, you can solve this problem.
Here is how I did it if you are interested.

Thanks @shimphillip. I’ve blurred the code now.

I did think of using ASCII code initially, but thought that would be less readable.

Didn’t know that indexOf uses a for loop --> thanks for the heads up.

How much of a performance issue does using nested loops cause vs conditionals? Is there a rule of thumb that I can follow while deciding between whether to use an extra loop?

There are few ways. One way is,

You would utilize hashmap aka objects in Javascript. With provided keys, it can instantly find corresponding values and performance is a big topic. I would search this on Google. :slight_smile: