Skip to content

Commit f53d78b

Browse files
authored
Update README.md
1 parent 85c4e02 commit f53d78b

File tree

1 file changed

+4
-89
lines changed

1 file changed

+4
-89
lines changed

README.md

Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -712,100 +712,15 @@ __code Implementation:__ <p> </p>
712712
<hr>
713713
<hr>
714714
715-
<b>9. Caesar Cipher </b>
715+
<b>9. Name </b>
716716
717-
__The challenge:__ <p> Given a string and a shift key, encrypt the string through Caesar Cipher.</p>
718-
719-
__Algorithmic Thinking:__ <p> This is a very simple algorithm that requires only a tiny bit of prerequisite knowledge regarding ASCII, and also some discretion when processing possible over 26 keys. Basically we just need to know that all chracters are stored as numbers in computer memory according to the ASCII standard: https://www.w3schools.com/charsets/ref_html_ascii.asp. And that the modulus function is our friend :)</p>
720-
721-
722-
__code Implementation:__ <p>
723-
724-
So first of all we need to split the string into characters to be processed individually and convert it to ASCII codes. Then we must check whether the character is either uppercase or lowercase(everything else should be kept the same) then add the key to it accordingly. But it is not a simple matter of simply doing ```char + key``` because for example, by shifting X by 3 we should get A. However, X(88) + 3 equals to 91 which is "\[". Thus what we should be doing is:
725-
726-
```js
727-
// js has built in String.charCodeAt() method to help us get the ASCII code
728-
// https://www.w3schools.com/jsref/jsref_charcodeat.asp
729-
// the reverse is String.fromCharCode()
730-
const char = charCodeAt("X")
731-
const key = 3
732-
733-
const encryptedWrongly = char + key
734-
735-
// uppercase letters start at code 65, ends at 90
736-
const encryptedCorrectly = 65 + (char + key) % 65
737-
738-
```
739-
740-
Also taking into account the possibility of >26 keys:
741-
742-
```js
743-
const encrypted = 65 + (char + key) % 65 % 26
744-
```
745-
746-
Additional functionality can be added by allowing the argument to be an array of keys:
747-
748-
```js
749-
const allShifts = [...Array(25).keys()].map( n => n + 1 )
750-
const cipher = (str, keys=allShifts) => {
751-
...
752-
const res = keys.map( key => {
753-
...
754-
})
755-
}
756-
```
717+
__The challenge:__ <p> </p>
757718
758-
Putting it all together:
759719
760-
```js
761-
const allShifts = [...Array(26).keys()]
762-
763-
// return all possible shifts if keys not given
764-
const cipher = (str, keys=allShifts) => {
765-
const chars = str.split("")
766-
const res = keys.map( key => {
767-
return chars.map( c => {
768-
const char = c.charCodeAt(0)
769-
if(char > 64 && char < 91)
770-
{
771-
return String.fromCharCode(65 + (char + key) % 65 % 26)
772-
}
773-
else if(char > 96 && char < 123)
774-
{
775-
return String.fromCharCode(97 + (char + key) % 97 % 26)
776-
}
777-
else
778-
{
779-
return String.fromCharCode(char)
780-
}
781-
})
782-
})
783-
return res.map( (r, i) => { return { key: keys[i], result: r.join("") } } )
784-
}
720+
__Algorithmic Thinking:__ <p> </p>
785721
786-
cipher("JavaScript", [1, 2, 27])
787-
/*[{ key: 1, result: 'KbwbTdsjqu' },
788-
{ key: 2, result: 'LcxcUetkrv' },
789-
{ key: 27, result: 'KbwbTdsjqu' }]*/
790-
791-
cipher("Something")
792-
/*[{ key: 1, result: 'Tpnfuijoh' },
793-
{ key: 2, result: 'Uqogvjkpi' },
794-
{ key: 3, result: 'Vrphwklqj' },
795-
{ key: 4, result: 'Wsqixlmrk' },
796-
......,
797-
{ key: 20, result: 'Migynbcha' },
798-
{ key: 21, result: 'Njhzocdib' },
799-
{ key: 22, result: 'Okiapdejc' },
800-
{ key: 23, result: 'Pljbqefkd' },
801-
{ key: 24, result: 'Qmkcrfgle' },
802-
{ key: 25, result: 'Rnldsghmf' }
803-
]
804-
*/
805-
```
806722
807-
And that's the easiest encryption ever! Happy Coding!
808-
</p>
723+
__code Implementation:__ <p> </p>
809724
<hr>
810725
<hr>
811726

0 commit comments

Comments
 (0)