diff --git a/caesar/caesar.js b/caesar/caesar.js index f4d6a25..7d12751 100644 --- a/caesar/caesar.js +++ b/caesar/caesar.js @@ -1,5 +1,20 @@ -const caesar = function() { +const caesar = function(string, shift) { + return string + .split("") + .map(char => shiftChar(char, shift)) + .join(""); +}; -} +let codeSets = code => (code < 97 ? 65 : 97); +let mode = (m, n) => (m % n + n) % n; + +let shiftChar =(char, shift) => { + let code = char.charCodeAt(); + + if((code >= 65 && code <= 90) || (code >= 97 && code <= 122)){ + return String.fromCharCode(mode(code + shift - codeSets(code), 26) + codeSets(code)); + } + return char; +}; module.exports = caesar diff --git a/caesar/caesar.spec.js b/caesar/caesar.spec.js index 49fe254..c4052cd 100644 --- a/caesar/caesar.spec.js +++ b/caesar/caesar.spec.js @@ -4,22 +4,22 @@ describe('caesar', function() { it('works with single letters', function() { expect(caesar('A', 1)).toEqual('B'); }); - xit('works with words', function() { + it('works with words', function() { expect(caesar('Aaa', 1)).toEqual('Bbb'); }); - xit('works with phrases', function() { + it('works with phrases', function() { expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!'); }); - xit('works with negative shift', function() { + it('works with negative shift', function() { expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!'); }); - xit('wraps', function() { + it('wraps', function() { expect(caesar('Z', 1)).toEqual('A'); }); - xit('works with large shift factors', function() { + it('works with large shift factors', function() { expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!'); }); - xit('works with large negative shift factors', function() { + it('works with large negative shift factors', function() { expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!'); }); });