fix: declared x variable (#36046)
This commit is contained in:
committed by
The Coding Aviator
parent
aad311a76f
commit
b013f2ab3a
@ -1,18 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: Caesars Cipher
|
title: Caesars Cipher
|
||||||
---
|
---
|
||||||
 Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code 
|
|
||||||
|
|
||||||
###  Problem Explanation:
|
|
||||||
|
|
||||||
* You need to write a function, which will take a 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'.
|
|
||||||
|
|
||||||
#### Relevant Links
|
|
||||||
|
|
||||||
* <a href='http://forum.freecodecamp.com/t/javascript-string-prototype-charcodeat/15933' target='_blank' rel='nofollow'>String.prototype.charCodeAt</a>
|
|
||||||
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode' target='_blank' rel='nofollow'>String.fromCharCode</a>
|
|
||||||
|
|
||||||
##  Hint: 1
|
##  Hint: 1
|
||||||
|
|
||||||
@ -40,13 +28,13 @@ Leave anything that doesn't come between A-Z as it is.
|
|||||||
|
|
||||||
##  Basic Code Solution:
|
##  Basic Code Solution:
|
||||||
```javascript
|
```javascript
|
||||||
function rot13(str) {
|
function rot13(str) {
|
||||||
// Split str into a character array
|
// Split str into a character array
|
||||||
return str.split('')
|
return str.split('')
|
||||||
// Iterate over each character in the array
|
// Iterate over each character in the array
|
||||||
.map.call(str, function(char) {
|
.map.call(str, function(char) {
|
||||||
// Convert char to a character code
|
// Convert char to a character code
|
||||||
x = char.charCodeAt(0);
|
var x = char.charCodeAt(0);
|
||||||
// Checks if character lies between A-Z
|
// Checks if character lies between A-Z
|
||||||
if (x < 65 || x > 90) {
|
if (x < 65 || x > 90) {
|
||||||
return String.fromCharCode(x); // Return un-converted character
|
return String.fromCharCode(x); // Return un-converted character
|
||||||
@ -58,9 +46,8 @@ Leave anything that doesn't come between A-Z as it is.
|
|||||||
// Otherwise shift the character 13 places backward
|
// Otherwise shift the character 13 places backward
|
||||||
return String.fromCharCode(x - 13);
|
return String.fromCharCode(x - 13);
|
||||||
}).join(''); // Rejoin the array into a string
|
}).join(''); // Rejoin the array into a string
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
 <a href='https://repl.it/CLjU/38' target='_blank' rel='nofollow'>Run Code</a>
|
|
||||||
|
|
||||||
### Code Explanation:
|
### Code Explanation:
|
||||||
|
|
||||||
@ -72,16 +59,16 @@ Leave anything that doesn't come between A-Z as it is.
|
|||||||
|
|
||||||
#### Relevant Links
|
#### Relevant Links
|
||||||
|
|
||||||
* <a href='http://forum.freecodecamp.com/t/javascript-array-prototype-map/14294' target='_blank' rel='nofollow'>Array.prototype.map</a>
|
* <a href='https://forum.freecodecamp.com/t/javascript-array-prototype-map/14294' target='_blank' rel='nofollow'>Array.prototype.map</a>
|
||||||
* <a href='http://forum.freecodecamp.com/t/javascript-string-prototype-split/15944' target='_blank' rel='nofollow'>String.prototype.split</a>
|
* <a href='https://forum.freecodecamp.com/t/javascript-string-prototype-split/15944' target='_blank' rel='nofollow'>String.prototype.split</a>
|
||||||
* <a href='http://forum.freecodecamp.com/t/javascript-array-prototype-join/14292' target='_blank' rel='nofollow'>Array.prototype.join</a>
|
* <a href='https://forum.freecodecamp.com/t/javascript-array-prototype-join/14292' target='_blank' rel='nofollow'>Array.prototype.join</a>
|
||||||
|
|
||||||
##  Intermediate Code Solution:
|
##  Intermediate Code Solution:
|
||||||
```javascript
|
```javascript
|
||||||
// Solution with Regular expression and Array of ASCII character codes
|
// Solution with Regular expression and Array of ASCII character codes
|
||||||
function rot13(str) {
|
function rot13(str) {
|
||||||
var rotCharArray = [];
|
var rotCharArray = [];
|
||||||
var regEx = /[A-Z]/ ;
|
var regEx = /[A-Z]/;
|
||||||
str = str.split("");
|
str = str.split("");
|
||||||
for (var x in str) {
|
for (var x in str) {
|
||||||
if (regEx.test(str[x])) {
|
if (regEx.test(str[x])) {
|
||||||
@ -95,10 +82,10 @@ Leave anything that doesn't come between A-Z as it is.
|
|||||||
}
|
}
|
||||||
str = String.fromCharCode.apply(String, rotCharArray);
|
str = String.fromCharCode.apply(String, rotCharArray);
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change the inputs below to test
|
// Change the inputs below to test
|
||||||
rot13("LBH QVQ VG!");
|
rot13("LBH QVQ VG!");
|
||||||
```
|
```
|
||||||
### Code Explanation:
|
### Code Explanation:
|
||||||
|
|
||||||
@ -146,8 +133,6 @@ Leave anything that doesn't come between A-Z as it is.
|
|||||||
* <a href='https://forum.freecodecamp.com/t/regular-expressions-resources/15931' target='_blank' rel='nofollow'>Regex</a>
|
* <a href='https://forum.freecodecamp.com/t/regular-expressions-resources/15931' target='_blank' rel='nofollow'>Regex</a>
|
||||||
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test' target='_blank' rel='nofollow'>Regex.test</a>
|
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test' target='_blank' rel='nofollow'>Regex.test</a>
|
||||||
|
|
||||||
 <a href='https://repl.it/CLjU/39' target='_blank' rel='nofollow'>Run Code</a>
|
|
||||||
|
|
||||||
##  Advanced Code Solution:
|
##  Advanced Code Solution:
|
||||||
|
|
||||||
function rot13(str) { // LBH QVQ VG!
|
function rot13(str) { // LBH QVQ VG!
|
||||||
|
Reference in New Issue
Block a user