Very new to coding, so please don't bully me.
Implement the legendary caesar cipher:
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
Question: Write a function that takes a string to be encoded and a shift factor and then returns the encoded string:
caesar('A', 1) // simply shifts the letter by 1: returns 'B' the cipher should retain capitalization:
caesar('Hey', 5) // returns 'Mjd; should not shift punctuation:
caesar('Hello, World!', 5) //returns 'Mjqqt, Btwqi!' the shift should wrap around the alphabet:
caesar('Z', 1) // returns 'A' negative numbers should work as well:
caesar('Mjqqt, Btwqi!', -5) // returns 'Hello, World!'
My Solution:
function caesar(string, num) {
let arr = [];
for(let i=0;i<string.length;i++)
{if(!(/[a-zA-Z]/.test(string[i])))
{arr[i]=string[i]; continue;}
let n = string.charCodeAt(i) + num;
if (string[i] == string[i].toLowerCase())
{if(n>122)
{while(n>122)
{n-=26;}}
else
{while(n<97)
{n+=26;}
}
}
else
{if(n>90)
{while(n>90)
{n-=26;}
}
else
{while(n<65)
{n+=26;}
}
}
arr[i]=String.fromCharCode(n);
}
console.log(arr.join(''));
}
caesar("Hello, World!", 2);
caesar("Hello, World!", 75);
The code is working perfectly as per requirement, but please help me with a better solution if possible.
And if you do, please use comments to extensively explain the working process, as I'm quite the noob.