commit
ca739f7a38
|
@ -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!'
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const caesar = function(value, addBy) {
|
const caesar = function(value, addBy) {
|
||||||
|
|
||||||
const lowerCaseAlphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
|
const lowerCaseAlphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
|
||||||
const upperCaseAlphabet = 'abcdefghijklmnopqrstuvwxyz'.toUpperCase().split('');
|
const upperCaseAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
|
||||||
const specialChars = [' ', '!', ','];
|
const specialChars = [' ', '!', ','];
|
||||||
|
|
||||||
const valueArray = value.split('');
|
const valueArray = value.split('');
|
||||||
|
@ -11,7 +11,14 @@ const caesar = function(value, addBy) {
|
||||||
|
|
||||||
if (lowerCaseAlphabet.includes(valueArray[i])) {
|
if (lowerCaseAlphabet.includes(valueArray[i])) {
|
||||||
let letterIndex = lowerCaseAlphabet.indexOf(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]);
|
newArr.push(lowerCaseAlphabet[finalIndex]);
|
||||||
} else if (upperCaseAlphabet.includes(valueArray[i])) {
|
} else if (upperCaseAlphabet.includes(valueArray[i])) {
|
||||||
|
@ -22,23 +29,10 @@ const caesar = function(value, addBy) {
|
||||||
} else if (specialChars.includes(valueArray[i])) {
|
} else if (specialChars.includes(valueArray[i])) {
|
||||||
newArr.push(valueArray[i]);
|
newArr.push(valueArray[i]);
|
||||||
} else {
|
} else {
|
||||||
// return 'is not a part of the alphabet';
|
return valueArray[i];
|
||||||
return 'Hello'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return newArr.join('');
|
return newArr.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = caesar;
|
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
|
|
||||||
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
|
@ -9,11 +9,11 @@ describe('caesar', 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!', 1)).toEqual('Ifmmp, Xpsme!');
|
||||||
});
|
});
|
||||||
|
|
||||||
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!');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -28,4 +28,8 @@ describe('caesar', function() {
|
||||||
xit('works with large negative shift factors', function() {
|
xit('works with large negative shift factors', function() {
|
||||||
expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!');
|
expect(caesar('Hello, World!', -29)).toEqual('Ebiil, Tloia!');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not work with a number', function() {
|
||||||
|
expect(caesar('1', 5)).toContain('1');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue