JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Hi, the code works but it doesn’t pass the test.

function rot13(str) {
str = str.split("");
let str1 = "";
let newArr = [];
let newArr1 = [];
for(let i = 0; i < str.length; i++){
  if(str[i].charCodeAt(0) >= 78 && str[i].charCodeAt(0) <= 90 || str[i].charCodeAt(0) == 32){
newArr.push(str[i].charCodeAt(0) - 13);
  } else if(str[i].charCodeAt(0) >= 65 && str[i].charCodeAt(0) <= 77){
    newArr.push(str[i].charCodeAt(0) + 13)
  } else {
    newArr.push(str[i].charCodeAt(0));
  }

}
for(let i = 0; i < newArr.length; i++){
  newArr1.push(String.fromCharCode(newArr[i]))
}

for(let i = 0; i < newArr1.length; i++){
  if(newArr1[i] == "\u0013"){
    str1+= " "
  } 
 str1+= newArr1[i];
}
console.log(str1)
return str1;
}

rot13("SERR PBQR PNZC");
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0

Challenge: JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Link to the challenge:

Note - it is easier if you use meaningful variable names and good formatting.

function rot13(str) {
  str = str.split("");
  let str1 = "";
  let newArr = [];
  let newArr1 = [];
  for (let i = 0; i < str.length; i++) {
    if (str[i].charCodeAt(0) >= 78 && str[i].charCodeAt(0) <= 90 || str[i].charCodeAt(0) == 32) {
      newArr.push(str[i].charCodeAt(0) - 13);
    } else if (str[i].charCodeAt(0) >= 65 && str[i].charCodeAt(0) <= 77) {
      newArr.push(str[i].charCodeAt(0) + 13)
    } else {
      newArr.push(str[i].charCodeAt(0));
    }

  }
  for (let i = 0; i < newArr.length; i++) {
    newArr1.push(String.fromCharCode(newArr[i]))
  }

  for (let i = 0; i < newArr1.length; i++) {
    if (newArr1[i] == "\u0013") {
      str1 += " "
    }
    str1 += newArr1[i];
  }
  console.log(str1);
  return str1;
}

console.log(rot13("SERR PBQR PNZC") == "FREE CODE CAMP");

Look at the result here. Your string results do not exactly match the target.

This is the problem

Ok, fixed the problem. Thanks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.