Solution
This commit is contained in:
parent
c61615f4bf
commit
25c0e43491
|
@ -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
|
module.exports = caesar
|
||||||
|
|
|
@ -4,22 +4,22 @@ describe('caesar', function() {
|
||||||
it('works with single letters', function() {
|
it('works with single letters', function() {
|
||||||
expect(caesar('A', 1)).toEqual('B');
|
expect(caesar('A', 1)).toEqual('B');
|
||||||
});
|
});
|
||||||
xit('works with words', function() {
|
it('works with words', function() {
|
||||||
expect(caesar('Aaa', 1)).toEqual('Bbb');
|
expect(caesar('Aaa', 1)).toEqual('Bbb');
|
||||||
});
|
});
|
||||||
xit('works with phrases', function() {
|
it('works with phrases', function() {
|
||||||
expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!');
|
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!');
|
expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
|
||||||
});
|
});
|
||||||
xit('wraps', function() {
|
it('wraps', function() {
|
||||||
expect(caesar('Z', 1)).toEqual('A');
|
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!');
|
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!');
|
expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue