# brunnock's solution

## to Rotational Cipher in the JavaScript Track

Published at Aug 25 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.

The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`. Using a key of `0` or `26` will always yield the same output due to modular arithmetic. The letter is shifted for as many values as the value of the key.

The general notation for rotational ciphers is `ROT + <key>`. The most commonly used rotational cipher is `ROT13`.

A `ROT13` on the Latin alphabet would be as follows:

``````Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
``````

It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.

Ciphertext is written out in the same formatting as the input including spaces and punctuation.

## Examples

• ROT5 `omg` gives `trl`
• ROT0 `c` gives `c`
• ROT26 `Cool` gives `Cool`
• ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
• ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`

## Setup

Go through the setup instructions for ECMAScript to install the necessary dependencies:

http://exercism.io/languages/ecmascript

## Requirements

Install assignment dependencies:

``````\$ npm install
``````

## Making the test suite pass

Execute the tests with:

``````\$ npm test
``````

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xtest` to `test`.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### rotational-cipher.spec.js

``````import RotationalCipher from './rotational-cipher';

describe('Rotational cipher', () => {
test('rotate a by 0, same output as input', () => {
const expected = 'a';
const actual = RotationalCipher.rotate('a', 0);

expect(actual).toEqual(expected);
});

xtest('rotate a by 1', () => {
const expected = 'b';
const actual = RotationalCipher.rotate('a', 1);

expect(actual).toEqual(expected);
});

xtest('rotate a by 26, same output as input', () => {
const expected = 'a';
const actual = RotationalCipher.rotate('a', 26);

expect(actual).toEqual(expected);
});

xtest('rotate m by 13', () => {
const expected = 'z';
const actual = RotationalCipher.rotate('m', 13);

expect(actual).toEqual(expected);
});

xtest('rotate n by 13 with wrap around alphabet', () => {
const expected = 'a';
const actual = RotationalCipher.rotate('n', 13);

expect(actual).toEqual(expected);
});

xtest('rotate capital letters', () => {
const expected = 'TRL';
const actual = RotationalCipher.rotate('OMG', 5);

expect(actual).toEqual(expected);
});

xtest('rotate spaces', () => {
const expected = 'T R L';
const actual = RotationalCipher.rotate('O M G', 5);

expect(actual).toEqual(expected);
});

xtest('rotate numbers', () => {
const expected = 'Xiwxmrk 1 2 3 xiwxmrk';
const actual = RotationalCipher.rotate('Testing 1 2 3 testing', 4);

expect(actual).toEqual(expected);
});

xtest('rotate punctuation', () => {
const expected = 'Gzo\'n zvo, Bmviyhv!';
const actual = RotationalCipher.rotate('Let\'s eat, Grandma!', 21);

expect(actual).toEqual(expected);
});

xtest('rotate all letters', () => {
const expected = 'Gur dhvpx oebja sbk whzcf bire gur ynml qbt.';
const actual = RotationalCipher.rotate('The quick brown fox jumps over the lazy dog.', 13);

expect(actual).toEqual(expected);
});
});``````
``````function rotateChar(char,amount,min,max) {
let charNum = char.charCodeAt(0);
if (charNum>=min && charNum<=max) {
charNum += amount;
if (charNum > max) charNum = (min-1) + charNum%max;
}
return(String.fromCharCode(charNum));
}
class RotationalCipher {
static rotate(input,amount) {
let inputArr = input.split('');
inputArr = inputArr.map(x=>rotateChar(x,amount,65,90));
inputArr = inputArr.map(x=>rotateChar(x,amount,97,122));
return inputArr.join('');
}
}
module.exports=RotationalCipher;``````