Skip to content

Instantly share code, notes, and snippets.

@holdenhinkle
Last active December 13, 2019 14:22
Show Gist options
  • Save holdenhinkle/92fa4a625be0c339e1fc70cf6bae9015 to your computer and use it in GitHub Desktop.
Save holdenhinkle/92fa4a625be0c339e1fc70cf6bae9015 to your computer and use it in GitHub Desktop.
This solution is wrong. I misunderstood the problem.
function railFenceCipher(operation, string, height = 3) {
function performOperation(operation, string, height, lines) {
let results = ''; // only for decrypt
let currentLine = 0;
let moveUp = true;
let moveDown = false;
for (let i = 0; i < lines[0].length; i += 1) {
if (operation === 'encrypt') {
lines[currentLine].splice(i, 1, string[i]);
} else if (operation === 'decrypt') {
results += lines[currentLine][i];
}
if ((moveUp && currentLine + 1 > height - 1) ||
(moveDown && currentLine - 1 < 0)) {
if (moveUp) currentLine -= 1;
if (moveDown) currentLine += 1;
moveUp = !moveUp;
moveDown = !moveDown;
} else if (moveUp) {
currentLine += 1;
} else if (moveDown) {
currentLine -= 1;
}
}
if (operation === 'encrypt') {
return lines.reduce((str, line) => str += line.join(' ') + '\n', '');
} else if (operation === 'decrypt') {
return results;
}
}
if (operation === 'encrypt' && height === 1) {
return 'Height must be 2 or more.';
}
if (operation === 'encrypt') {
string = string.replace(/\W/g, '');
lines = [...Array(height)].map((_, i) => i)
.map(line => [...Array(string.length)].map((_, i) => i))
.map(line => line.map(element => '.'));
} else if (operation === 'decrypt') {
string = string.replace(/\n$/, '');
lines = string.split('\n').map(line => line.split(' '));
height = lines.length;
}
return performOperation(operation, string, height, lines);
}
// each lines ends with \n
cipher = 'W . . . E . . . C . . . R . . . L . . . T . . . E\n. E . R . D . S . O . E . E . F . E . A . O . C .\n. . A . . . I . . . V . . . D . . . E . . . N . .\n';
console.log(railFenceCipher('decrypt', cipher));
// last line doesn't end with \n
cipher = 'W . . . E . . . C . . . R . . . L . . . T . . . E\n. E . R . D . S . O . E . E . F . E . A . O . C .\n. . A . . . I . . . V . . . D . . . E . . . N . .';
console.log(railFenceCipher('decrypt', cipher));
// only print letters
message = 'WE ARE DISCOVERED FLEE AT ONCE';
console.log(railFenceCipher('encrypt', message, 3));
// only encrypts print letters
message = 'WE @#$ARE DISCOVERED -----FLEE ~~~AT ^&*(ONCE';
console.log(railFenceCipher('encrypt', message, 5));
// works with no height (3 is default)
message = 'I LOVE LAUNCH SCHOOL';
console.log(railFenceCipher('encrypt', message));
// doesn't work with height 1
message = 'I LOVE LAUNCH SCHOOL';
console.log(railFenceCipher('encrypt', message, 1));
message = 'I LOVE LAUNCH SCHOOL';
console.log(railFenceCipher('encrypt', message, 2));
message = 'I LOVE LAUNCH SCHOOL';
console.log(railFenceCipher('encrypt', message, 3));
message = 'I LOVE LAUNCH SCHOOL';
console.log(railFenceCipher('encrypt', message, 4));
message = 'I LOVE LAUNCH SCHOOL';
console.log(railFenceCipher('encrypt', message, 10));
message = 'I LOVE LAUNCH SCHOOL';
console.log(railFenceCipher('encrypt', message, 20));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment