Caesars Cipher - Need Help

Caesars Cipher - Need Help
0

#1

Tell us what’s happening:

I have gotten this far. I am able to convert the values to their respective Rot13 values. But I cannot work with the Spaces between the “Free Code Camp” and the special characters in other tests.
I need someone to help me with it.

Your code so far


function rot13(str) { // LBH QVQ VG!
  
  let result = [];
    let test = str.split("");
  console.log(test);

  let arr = ['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 testArr = arr.slice();
  console.log(arr);

  let arr2 = [];

  for (var i = 0; i < 13; i++) {
    arr2.push(arr[i+13]);
  }

  arr2 = arr2.concat(testArr.splice(0, 13));

console.log(arr2);

test.map(function(item) {
  for (var j = 0; j < arr.length; j++) {
    if (item == arr[j]) {
      result.push(arr2[j]);
    }
  }

  return result;
})

return result;
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/caesars-cipher


#2

If a character isn’t in your arr you could just not change it.


#3

You mean to say that I will have to change the way I wrote this program? Is there nothing I can do to improve it? Because I am able to convert the text to their respective values.


#4

Don’t panic. You know how to write if statements. It’s possble to check whether a value is in an array. You can do this.


#5

Thanks for the hint. I was able to solve it finally.

function rot13(str) { // LBH QVQ VG!
  let result = [];
    let test = str.split("");
  console.log(test);

  let arr = ['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 testArr = arr.slice();
  console.log(arr);

  let arr2 = [];

  for (var i = 0; i < 13; i++) {
    arr2.push(arr[i+13]);
  }

  arr2 = arr2.concat(testArr.splice(0, 13));

console.log(arr2);

test.map(function(item) {
  for (var j = 0; j < arr.length; j++) {
    if (item == arr[j]) {
      result.push(arr2[j]);
    }
  }
  if (!arr.includes(item)) {
    result.push(item);
  }
  return result;
})

return result.join("");

}

// Change the inputs below to test
rot13("SERR PBQR PNZC");