Skip to content

Instantly share code, notes, and snippets.

@MrBisquit
Last active August 15, 2023 07:22
Show Gist options
  • Save MrBisquit/bc673d9b3da6dbc8c4cdaf5006cba18b to your computer and use it in GitHub Desktop.
Save MrBisquit/bc673d9b3da6dbc8c4cdaf5006cba18b to your computer and use it in GitHub Desktop.
Caesar Cipher | DDS Code Challenge
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
function caesarCipher(string, shiftValue) {
var lettersArray = __spreadArray([], Array(26), true).map(function (_, index) { return String.fromCharCode(97 + index); }); // Lowercase letters a-z
lettersArray.push.apply(// Lowercase letters a-z
lettersArray, __spreadArray([], Array(26), true).map(function (_, index) { return String.fromCharCode(65 + index); })); // Uppercase letters A-Z
// Thanks ChatGPT for the above code.
var splitString = string.split('');
var newString = "";
splitString.forEach(function (char) {
if (char == ' ')
return newString += ' ';
for (var i = 0; i < lettersArray.length; i++) {
if (char == lettersArray[i]) {
newString += lettersArray[i + shiftValue];
}
}
});
return newString;
}
function caesarCipher(string: string, shiftValue: number) {
const lettersArray: string[] = [...Array(26)].map((_, index) => String.fromCharCode(97 + index)); // Lowercase letters a-z
lettersArray.push(...[...Array(26)].map((_, index) => String.fromCharCode(65 + index))); // Uppercase letters A-Z
// Thanks ChatGPT for the above code.
let splitString:string[] = string.split('');
let newString:string = "";
splitString.forEach(char => {
if(char == ' ') return newString += ' ';
for (let i = 0; i < lettersArray.length; i++) {
if(char == lettersArray[i]) {
newString += lettersArray[i + shiftValue];
}
}
});
return newString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment