-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3817 from Othamae/main
Reto #24 - javascript
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Retos/Reto #24 - CIFRADO CÉSAR [Fácil]/javascript/othamae.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Crea un programa que realize el cifrado César de un texto y lo imprima. | ||
* También debe ser capaz de descifrarlo cuando así se lo indiquemos. | ||
* | ||
* Te recomiendo que busques información para conocer en profundidad cómo | ||
* realizar el cifrado. Esto también forma parte del reto. | ||
*/ | ||
|
||
function cesarCipher (text, number){ | ||
let cipher = '' | ||
for (let i = 0; i < text.length; i++){ | ||
let char = text.charCodeAt(i) | ||
if (char >= 65 && char <= 90){ | ||
char = ((char - 65 + number+26) % 26) + 65 | ||
} | ||
else if (char >= 97 && char <= 122){ | ||
char = ((char - 97 + number+26) % 26) + 97 | ||
} | ||
cipher += String.fromCharCode(char) | ||
} | ||
return cipher; | ||
} | ||
|
||
function cesarDecipher (text, number){ | ||
return cesarCipher(text, - number) | ||
} | ||
|
||
console.log(cesarCipher('Hola mundo!', 3)) | ||
console.log(cesarDecipher('Krod pxqgr!', 3)) | ||
|
||
console.log(cesarCipher('Hola mundo!', 9)) | ||
console.log(cesarDecipher('Qxuj vdwmx!', 9)) | ||
|