-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCesarCipher.js
36 lines (26 loc) · 946 Bytes
/
CesarCipher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//cesarCipher
//Shift every letter in the given string by a number.
function cesarCipher(str, shift){
//Ill start be making this work for one character and then build up to a string.
//create an array of the alphabet
const alphaArr = 'abcdef'.split(''); //[a,b,c,d...]
end=alphaArr.length-1;
console.log(`end=${end}`);
//need the index of the originating letter.
const index = alphaArr.indexOf(str); //a=0, b=1, z=25
//move forward in the array, starting at the index of the str til we reach the num
console.log(`index=${index}`);
//fast exit
if (shift===0){
return char;
}
let total = index + end;
console.log(`total=${total}`);
let newShift = total - shift;
console.log(`newshift=${newShift}`);
if (newShift > 0){
return (alphaArr[newShift-1]);
}
}
let answer = cesarCipher('c', 5);
console.log(answer);