From b1ac34ced1d98edcc8db465b2054ea071200677f Mon Sep 17 00:00:00 2001 From: billalp Date: Fri, 1 Nov 2019 11:45:15 +0000 Subject: [PATCH] Latest changes --- caesar/README.md | 33 --------------------------------- caesar/caesar.js | 26 ++++++++++---------------- caesar/caesar.spec.js | 10 +++++++--- 3 files changed, 17 insertions(+), 52 deletions(-) delete mode 100644 caesar/README.md diff --git a/caesar/README.md b/caesar/README.md deleted file mode 100644 index 02d39a7..0000000 --- a/caesar/README.md +++ /dev/null @@ -1,33 +0,0 @@ -# Exercise XX - caesar cipher - -Implement the legendary caesar cipher: - -> In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence. - -write a function that takes a string to be encoded and a shift factor and then returns the encoded string: - -```javascript -caesar('A', 1) // simply shifts the letter by 1: returns 'B' -``` - -the cipher should retain capitalization: -```javascript -caesar('Hey', 5) // returns 'Mjd; -``` - -should _not_ shift punctuation: -```javascript -caesar('Hello, World!', 5) //returns 'Mjqqt, Btwqi!' -``` - -the shift should wrap around the alphabet: -```javascript -caesar('Z', 1) // returns 'A' -``` - -negative numbers should work as well: -```javascript -caesar('Mjqqt, Btwqi!', -5) // returns 'Hello, World!' -``` - - diff --git a/caesar/caesar.js b/caesar/caesar.js index 0591ae6..dfc3eac 100644 --- a/caesar/caesar.js +++ b/caesar/caesar.js @@ -1,7 +1,7 @@ const caesar = function(value, addBy) { const lowerCaseAlphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); - const upperCaseAlphabet = 'abcdefghijklmnopqrstuvwxyz'.toUpperCase().split(''); + const upperCaseAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); const specialChars = [' ', '!', ',']; const valueArray = value.split(''); @@ -11,7 +11,14 @@ const caesar = function(value, addBy) { if (lowerCaseAlphabet.includes(valueArray[i])) { let letterIndex = lowerCaseAlphabet.indexOf(valueArray[i]); - let finalIndex = letterIndex + addBy; + var finalIndex; + if (addBy < 0) { + // var finalIndex = letterIndex - addBy; + finalIndex = letterIndex - parseInt(addBy); + return finalIndex; + } else { + finalIndex = letterIndex + addBy; + } newArr.push(lowerCaseAlphabet[finalIndex]); } else if (upperCaseAlphabet.includes(valueArray[i])) { @@ -22,23 +29,10 @@ const caesar = function(value, addBy) { } else if (specialChars.includes(valueArray[i])) { newArr.push(valueArray[i]); } else { - // return 'is not a part of the alphabet'; - return 'Hello' + return valueArray[i]; } } return newArr.join(''); } module.exports = caesar; - -/* Pseudocode -1 Take input and place in an array - then we have an array of letters -2 If not a letter then ignore - DONE -3 If a space or ! then return space or ! - DONE -4 If it is a letter, it comes to the end then keep going around -5 -6 -7 Factor in minus - - -*/ diff --git a/caesar/caesar.spec.js b/caesar/caesar.spec.js index a598ef7..758218e 100644 --- a/caesar/caesar.spec.js +++ b/caesar/caesar.spec.js @@ -9,11 +9,11 @@ describe('caesar', function() { expect(caesar('Aaa', 1)).toEqual('Bbb'); }); - xit('works with phrases', function() { - expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!'); + it('works with phrases', function() { + expect(caesar('Hello, World!', 1)).toEqual('Ifmmp, Xpsme!'); }); - xit('works with negative shift', function() { + it('works with negative shift', function() { expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!'); }); @@ -28,4 +28,8 @@ describe('caesar', function() { xit('works with large negative shift factors', function() { expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!'); }); + + it('does not work with a number', function() { + expect(caesar('1', 5)).toContain('1'); + }); });