# JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Tell us what’s happening:
Hello everyone! My code doesn’t pass. I dont know why, it’s correct in my visual studio code. Help me please!!!

``````   **Your code so far**
``````
``````let listOne = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M']
let listTwo = ['N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
let encrypted = []
let j = 0
let strInUppercase = ''
let regex = /\W|_/

function rot13(str) {
strInUppercase = str.toUpperCase()
for (let i = 0; i < listOne.length; i++) {
if (strInUppercase[j] == listOne[i]) {
encrypted.push(listTwo[i])
j++
i = -1
} else if (strInUppercase[j] == ' ') {
encrypted.push(' ')
j++
i = -1
} else if (strInUppercase[j] == listTwo[i]) {
encrypted.push(listOne[i])
j++
i = -1
} else if (regex.test(strInUppercase[j])) {
encrypted.push(strInUppercase[j])
j++
i = -1
}
}

return encrypted.join("");
}

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

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36`

Challenge: JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

``````var myGlobal = [1];
function returnGlobal(arg) {
myGlobal.push(arg);
return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
var myLocal = [1];
myLocal.push(arg);
return myLocal;
} // reliable - always returns an array of length 2
``````

Side notes - you should use `const` here. Also, you should use meaningful names. `listOne` doesn’t tell me anything about what is inside of that variable.

It was solved! Thank you! I will change the variables’s name.

1 Like

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