Skip to content

Commit

Permalink
Cipher now ignores [,()]
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul Blanco Tejero committed Jul 16, 2019
1 parent 5032523 commit 2567b68
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions js/vigenere.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var POLYALPHABET = [];
const POLYALPHABET = [];

export default POLYALPHABET;

Expand All @@ -11,6 +11,8 @@ for (let row = 0; row < 26; row++) {
POLYALPHABET.push(rowLetters);
}

const reIgnoreChars = /[\s¡!¿?.,()]/;

export function encrypt(key, phrase) {
key = key.toUpperCase().replace(/\s/g, '');
phrase = phrase.toUpperCase().trim();
Expand All @@ -19,7 +21,7 @@ export function encrypt(key, phrase) {
for (i = 0, n = 0; i < phrase.length; i++, n++) {
let phraseChar = phrase.charAt(i);

if (/[\s¡!¿?.]/.test(phraseChar)) {
if (reIgnoreChars.test(phraseChar)) {
encrypted += phraseChar;
n--;
} else {
Expand All @@ -38,7 +40,7 @@ export function decrypt(key, encrypted) {
let encryptedChar = encrypted.charAt(i);
let lineTarget = POLYALPHABET[key.charCodeAt(n % key.length) - 65];

if (/[\s¡!¿?.]/.test(encryptedChar)) {
if (reIgnoreChars.test(encryptedChar)) {
phrase += encryptedChar;
n--;
} else
Expand Down

0 comments on commit 2567b68

Please sign in to comment.