# JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Tell us what’s happening:
My Caesar Cipher is only working for every odd character in my parameter; the result should be “FREE CODE CAMP”, however, I’m getting “FEER CBDR CNMC”.

I may have been a bit messy with my forEach() loops, but I cannot at all figure out why this is happening.

``````  **Your code so far**
``````
``````function rot13(str) {
// Alphabet Array
const alpha = Array
.from(Array(26)).map((e, i) => i + 65);
const alphabet = alpha
.map((x) => String.fromCharCode(x));

let splitArr = str.split("");

let charRegex = /[a-zA-Z]/gi;

let newArr = [];
splitArr.forEach(char => {
if (charRegex.test(char) === true) {
alphabet.forEach(charTwo => {
if(char === charTwo) {
console.log(alphabet.indexOf(charTwo))
newArr
.push(alphabet[(alphabet.indexOf(char)) - 13])
}
})
} else {
newArr.push(char);
}
})
console.log(newArr.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.5112.102 Safari/537.36 OPR/90.0.4480.54`

Challenge: JavaScript Algorithms and Data Structures Projects - Caesars Cipher

After you change A to N, what do you do to N?

I push the character onto newArr.

I usually check the result using console.log() before I actually submit the answer.

Blockquote

You’ve got a hard to discern bug…

I put these logs in your forEach:

``````  splitArr.forEach(char => {
if (charRegex.test(char) === true) {
console.log('if', char);
alphabet.forEach(charTwo => {
if (char === charTwo) {
newArr
.push(alphabet[(alphabet.indexOf(char)) - 13])
}
})
} else {
console.log('else', char);
newArr.push(char);
}
})
``````

And sure enough the output shows that it alternates on each character:

``````if S
else E
if R
else R
else
if P
else B
if Q
else R
else
if P
else N
if Z
else C
``````

It turns out you should not use the `g` flag on your regular expression when using `.test()`

Okay I figured it out. It wasn’t anything wrong with the code, I just didn’t know I would have to alternate between +13 and -13 characters.
TL; DR I’m a bit of an idiot