Skip to content
Browse files

Update to follow new template and add one more solution

  • Loading branch information...
1 parent 57ab7cb commit 78a2bdf4cd6573977aed109f9074312acc57d686 @vaskezu vaskezu committed with vaskezu
Showing with 79 additions and 27 deletions.
  1. +79 −27 Algorithm-Caesars-Cipher.md
View
106 Algorithm-Caesars-Cipher.md
@@ -1,28 +1,36 @@
-# Problem Explanation:
-- You need to write a function, which will take string encoded with
+:triangular_flag_on_post: Remember to use [**`Read-Search-Ask`**](http://github.com/FreeCodeCamp/FreeCodeCamp/wiki/How-to-get-help-when-you-get-stuck) if you get stuck. Try to pair program. :busts_in_silhouette: Write your own code. :memo:
+
+# :checkered_flag: Problem Explanation:
+> - You need to write a function, which will take string encoded with
*Caesar cipher* as a parameter and decode it.
- The one used here is ROT13 where the value of the letter is
shifted by 13 places.
e.g. 'A' ↔ 'N', 'T' ↔ 'G'.
- You have to shift it back 13 positions, such that 'N' ↔ 'A'.
-## Hint: 1
-Use *String.charCodeAt()* to convert the English character to ASCII.
-## Hint: 2
-Use *String.fromCharCode()* to convert ASCII to English character.
+## :speech_balloon: Hint: 1
+> Use *String.charCodeAt()* to convert the English character to ASCII.
+
+*try to solve the problem now*
+
+## :speech_balloon: Hint: 2
+> Use *String.fromCharCode()* to convert ASCII to English character.
+
+*try to solve the problem now*
+
+## :speech_balloon: Hint: 3
+> Leave anything that doesn't come between A-Z as it is.
-## Hint: 3
-Leave anything that doesn't come between A-Z as it is.
+*try to solve the problem now*
## Spoiler Alert!
[![687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif](https://files.gitter.im/FreeCodeCamp/Wiki/nlOm/thumb/687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif)](https://files.gitter.im/FreeCodeCamp/Wiki/nlOm/687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif)
**Solution ahead!**
-## Code Solution:
-#### First solution
+## :beginner: Basic Code Solution:
```js
function rot13(str) {
@@ -48,7 +56,56 @@ function rot13(str) {
}
```
-#### Second solution (advanced)
+### Code Explanation:
+> - A string variable `nstr` is declared and initialized to store the
+decoded string.
+- The for loop is used to loop through each character of the input string.
+- If the character is not uppercase English alphabets(i.e. its ascii doesn't lie between 65 and 91 ), we'll leave it
+as it is and [continue](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue) with next iteration.
+- If it's the uppercase English alphabet, we'll subtract 13 from it's
+ascii code.
+- If the ascii code is less than 78, it'll get out of
+range when subtracted by 13 so we'll add 26 (number of letters in English alphabets) to it so that after A it'll go back to Z.
+e.g. M(77) ↔ 77-13 = 64(Not an English alphabet) +26 = 90 ↔ Z(90)
+
+
+## :sunflower: Intermediate Code Solution:
+
+```js
+//Solution with Regular expression and Array of ASCII character codes
+function rot13(str) {
+ //retCharArray is an Array of character codes for the solution
+ var rotCharArray = [];
+ //regular expression for all upper case letter from A to Z
+ var regEx = /[A-Z]/ ;
+ //split str into a character array
+ str = str.split("");
+ //iterate over each character in the array
+ for (var x in str) {
+ //regEx.test(str[x]) will return (true or false) if it maches the regEx or not
+ if (regEx.test(str[x])) {
+ //checks if the new character code is greater or equal to 65 (letter A)
+ if (str[x].charCodeAt()-13 >= 65) {
+ rotCharArray.push(str[x].charCodeAt()-13);
+ } else {
+ rotCharArray.push(str[x].charCodeAt()+13);
+ }
+ } else {
+ rotCharArray.push(str[x].charCodeAt());
+ }
+ }
+ //make a string with character codes from an array of character codes
+ str = String.fromCharCode.apply(String, rotCharArray);
+ return str;
+}
+
+
+// Change the inputs below to test
+rot13("LBH QVQ VG!");
+```
+
+
+## :rotating_light: Advanced Code Solution:
```js
function rot13(str) {
@@ -72,21 +129,16 @@ function rot13(str) {
}
```
-# Code Explanation:
-#### First solution
-- A string variable `nstr` is declared and initialized to store the
-decoded string.
-- The for loop is used to loop through each character of the input string.
-- If the character is not uppercase English alphabets(i.e. its ascii doesn't lie between 65 and 91 ), we'll leave it
-as it is and [continue](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue) with next iteration.
-- If it's the uppercase English alphabet, we'll subtract 13 from it's
-ascii code.
-- If the ascii code is less than 78, it'll get out of
-range when subtracted by 13 so we'll add 26(number of letters in
- English alphabets) to it so that after A it'll go back to Z.
-e.g. M(77) ↔ 77-13 = 64(Not an English alphabet) +26 = 90 ↔ Z(90)
-# Credits:
-If you found this page useful, you can give thanks by copying and pasting this on the main chat: **`thanks @anuragaryan and @SaintPeter for your help with Algorithm: Caesar's Cipher`**
+### :trophy: Credits:
+If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:
+
+**`thanks @anuragaryan @SaintPeter @vaskezu for your help with Algorithm: Caesar's Cipher`**
+
+## :clipboard: NOTE TO CONTRIBUTORS:
+- :warning: **DO NOT** add solutions that are similar to any existing solutions. If you think it is ***similar but better***, then try to merge (or replace) the existing similar solution.
+- Add an explanation of your solution.
+- Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**. :traffic_light:
+- Please add your username only if you have added any **relevant main contents**. (:warning: ***DO NOT*** *remove any existing usernames*)
-> **NOTE:** Please add your username only if you have added any **relevant main contents** to the wiki page. (Please don't remove any existing usernames.)
+> See :point_right: [**`Challenge Solution Template`**](Challenge-Solution-Template) for reference.

0 comments on commit 78a2bdf

Please sign in to comment.
Something went wrong with that request. Please try again.