-
Notifications
You must be signed in to change notification settings - Fork 116
Solución Caesar cipher - Daniela Gonzales #74
base: master
Are you sure you want to change the base?
Conversation
@developerVilchez |
function cipher(myString) { | ||
var myStringCipher = ''; // almacenar el valor del string Cifrado | ||
|
||
for (var i = 0 ; i < myString.length ; i++) { // recorrer el string del usuario |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generalmente, los espacios al rededor del semicolon son como los de las comas. No hay espacio antes, pero si hay uno despues. Por favor quitar espacio antes de los semicolons en esta linea.
var numberOfTheLetter = myString.charCodeAt(i); // numero de la letra en el codigo ASCII | ||
var cipherFormula; // almacena fórmula de cifrado que se utiliza | ||
var theNewLetter; // alamacena valor de la nueva letra cifrada | ||
if (numberOfTheLetter >= 65 && numberOfTheLetter <= 90) { // valor UNICODE de letras mayúsculas en ASCII |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Para hacer los ifs mas legibles, cambiaria los numeros como 65, 90, 97, 122, y 32 con las letras seguidas por charCodeAt(0)
.
Por ejemplo, el primer if podria verse asi: numberOfTheLetter >= 'A'.charCodeAt(0) && numberOfTheLetter <= 'Z'.charCodeAt(0)
Lo mismo aplica para los otros if.
theNewLetter = String.fromCharCode(cipherFormula); // obtener el valor de la letra cifrada | ||
myStringCipher += theNewLetter; // formar el string cifrado | ||
} else if (numberOfTheLetter >= 97 && numberOfTheLetter <= 122) { // valor UNICODE de letras minúsculas en ASCII | ||
cipherFormula = (numberOfTheLetter - 97 + 33) % 26 + 97; // fórmula de cifrado Cesar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
el mismo comentario que en los ifs.
Actualiza el 97 a 'a'.charCodeAt(0) para mejorar legibilidad
|
||
for (var i = 0 ; i < myString.length ; i++) { // recorrer el string del usuario | ||
var numberOfTheLetter = myString.charCodeAt(i); // numero de la letra en el codigo ASCII | ||
var cipherFormula; // almacena fórmula de cifrado que se utiliza |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
La razon por la cual les damos nombres descriptivos para nuestros variables es para no necesitar comentarios explicando que hacen. Favor de borrar los comentarios que no sean necesarios.
} else if (numberOfTheLetter === 32) { // verificar si es un espacio vacio | ||
myStringCipher += ' '; // añadir espacio en string cifrado | ||
} else { | ||
break; // Si no es una letra detente |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
en este caso, el break se sale del loop, pero aun vamos a llegar a la linea de return myStringCipher. Creo que las indicaciones dicen que tenemos que prevenir que el usuario ponga un mensaje que tenga algo que no sea letra o espacio. Por favor enseniarle un mensaje al usuario y no regresar nada.
// funcion para descifrar una cadena de texto | ||
function decipher(myString) { | ||
var myStringDechiper = ''; // alamacenar el valor del string decifrado | ||
for (var i = 0; i < myString.length ; i++) { // recorrer el string del usuario |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quitar espacio antes del segundo semicolon
var numberOfTheLetter = myString.charCodeAt(i); // valor UNICODE de la letra en el código ASCII | ||
var decipherFormula; // almacena fórmula de decifrado | ||
var theNewLetter; // almacena el nuevo valor de la letra decifrada | ||
if (numberOfTheLetter >= 65 && numberOfTheLetter <= 90) { // saber si la letra está en mayúscula |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mismo comentario que hice en la otra function
var userText; // almacenar el texto que el usuario ingreso. | ||
var verification; // verificar que inicie con letras | ||
|
||
switch (true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Para el switch, estas checando el valor de tu variable "option" asi que en vez de switch (true) puedes poner switch (option) y ya en cada case solamente tienes que poner case ('1') y case ('2')
switch (true) { | ||
case (option === '1'): | ||
userText = prompt('¿Qué texto desea cifrar? (Por favor ingresar un texto)'); | ||
verification = parseInt(userText); // parsear para verificar que una cadena de texto |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creo que seria mejor tener un helper function que te verifique que solamente haya letras y espacios en el userText, en vez de usar parseInt
userText = prompt('¿Qué texto desea cifrar? (Por favor ingresar un texto)'); | ||
verification = parseInt(userText); // parsear para verificar que una cadena de texto | ||
// Si la cadena inicia con números volver a pedir un texto para cifrar | ||
while (verification >= 0 || verification <= 0 || userText === '') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
con el helper function que estoy explicando, regresarias un bool y podrias checar:
While (!verify(userText))
Hola , soy Daniela y le envio mi solución del reto de código 'Caesar cipher' del bootcamp de Laboratoria.
Espero su comentario respecto al código , muchas gracias por su tiempo .