Please, can experienced developers help point out my error(s) on this Caeser’s Cipher project? I have retrieved decoded values from the Cipher input values but my challenge is that I want to account for the non-alphabtic characters and the return the value of the decoded Cipher. When I try to push none-alphabetic characters to the characters decoded, the characters are pushed more than once, example is when it gets to an empty string which it is supposed to push the empty string once so that when I join the Decoded array to string, the empty string is converted to space in the words. Same goes for other characters that are not empty string like (!,.) etc.
function rot13(str) { // LBH QVQ VG!
//encode a list of the english alphabets and set it to alpha
const alpha = ['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'];
//encode a list of the caeser's cipher and set it to cipher
const cipher = ['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'];
//get the input values in encoded cipher and split to individual items
const inputStr = str.split('');
let regEX = /\W|\s/;
//create an empty container to hold the index values of found alpha letters and set it to alphaIndex;
const alphaIndex = [];
//iterate over the inputStr value to acess each item
for(let i = 0; i < inputStr.length; ++i){
//check if an item is a string from english letter alphabets
let index = alpha.indexOf(inputStr[i]);
// console.log(index);
//if letter is english letter alpha, take the index of the letter ocurrence and store it on the decodedIndex
if(index >= 0){
// console.log(inputStr[i]);
alphaIndex.push(index);
} else {
alphaIndex.push(inputStr[i]);
}
} // end of iteration for inputStr
//TESTING ALPHAINDEX
console.log(alphaIndex);
//create a container to hold the decoded values and set it to decoded
const decoded = [];
//iterate over the length of alphaIndex values to acess individual item
for(let i = alphaIndex.length; i > 0; --i){
let popped = alphaIndex.shift();
// console.log(popped) //take out the first values of alphaIndex
for(let j = 0; j < cipher.length; ++j){
if(popped === j){
decoded.push(cipher[j]);
}//
////////////////THIS IS WHERE I HAVE THE HUGE CHALLENGE
if(regEX.test(popped)){
decoded.push(popped);
}//
}
}
// console.log(alphaIndex);
console.log(decoded.join(''));
// console.log(cipher[18]+cipher[4]+cipher[17]+cipher[17]+' '+ cipher[15]+cipher[1]+cipher[16]+cipher[17]+' '+cipher[15]+cipher[13]+cipher[25]+cipher[2])
return str;
}
// Change the inputs below to test
// rot13("SERR PBQR PNZC");
rot13("SERR CVMMN!");
// rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.")