UncategorizedCaesar Cipher
Fork
Share
Fullscreen
Sign In
JavaScript
C++
Java
Building
Play
0 / 1
Speed
0
2
4
README.md
code.js
// import visualization libraries {...}
const string = 'hello! how are you doing?';
const rotation = 5;
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
// create a map of char -> position to improve run time
// otherwise we would have to search the alphabet each
// time to find the character position
const alphabetMap = alphabet.split('').reduce((map, curr, idx) => {
map[curr] = idx;
return map;
}, {});
// define tracer variables {...}
function getPosUp(pos) {
return (pos === alphabet.length - 1) ? 0 : pos + 1;
}
function getPosDown(pos) {
return (pos === 0) ? alphabet.length - 1 : pos - 1;
}
function getNextChar(currChar, direction) {
const pos = alphabetMap[currChar];
const nextPos = direction === 'up' ? getPosUp(pos) : getPosDown(pos);
const nextChar = alphabet.charAt(nextPos);
// logger {...}
return nextChar;
}
function cipher(str, rotation, direction, cipherTracer) {
if (!str) return '';
for (let i = 0; i < str.length; i++) {
// visualize {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Contributed by
nem035
64json
archie94
Yee172
Delete File