From 00f5990fc82fc796f9afb953c7a71a3c746d1e06 Mon Sep 17 00:00:00 2001 From: Veronica Conesa Date: Tue, 13 Jun 2023 10:08:13 +0200 Subject: [PATCH 1/2] Reto #24 - javascript --- .../javascript/othamae.js" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 "Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/javascript/othamae.js" diff --git "a/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/javascript/othamae.js" "b/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/javascript/othamae.js" new file mode 100644 index 0000000000..8e89cccb86 --- /dev/null +++ "b/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/javascript/othamae.js" @@ -0,0 +1,29 @@ +/* + * 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) + 65 + } + else if (char >= 97 && char <= 122){ + char = ((char - 97 + number) % 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)) From 7f69f3352bd0e6bd7222ec60c78f905d9fbc12f5 Mon Sep 17 00:00:00 2001 From: Veronica Conesa Date: Tue, 13 Jun 2023 10:33:17 +0200 Subject: [PATCH 2/2] Fixed decipher --- .../javascript/othamae.js" | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git "a/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/javascript/othamae.js" "b/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/javascript/othamae.js" index 8e89cccb86..a5de9864cc 100644 --- "a/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/javascript/othamae.js" +++ "b/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/javascript/othamae.js" @@ -9,12 +9,12 @@ function cesarCipher (text, number){ let cipher = '' for (let i = 0; i < text.length; i++){ - let char = text.charCodeAt(i); + let char = text.charCodeAt(i) if (char >= 65 && char <= 90){ - char = ((char - 65 + number) % 26) + 65 + char = ((char - 65 + number+26) % 26) + 65 } else if (char >= 97 && char <= 122){ - char = ((char - 97 + number) % 26) + 97 + char = ((char - 97 + number+26) % 26) + 97 } cipher += String.fromCharCode(char) } @@ -22,8 +22,12 @@ function cesarCipher (text, number){ } function cesarDecipher (text, number){ - return cesarCipher(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)) +