-
Notifications
You must be signed in to change notification settings - Fork 116
Cifrado Cesar de María Alejandra Cabrera Pérez #69
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "parserOptions": { | ||
| "ecmaVersion": 6 | ||
| }, | ||
| "rules": { | ||
| "keyword-spacing": 1, | ||
| "space-before-function-paren": [1, "never"], | ||
| "eqeqeq": 1, | ||
| "space-infix-ops": 1, | ||
| "comma-spacing": 1, | ||
| "brace-style": 1, | ||
| "no-multiple-empty-lines": 1, | ||
| "camelcase": 1, | ||
| "func-call-spacing": 1, | ||
| "key-spacing": 1, | ||
| "semi": 1, | ||
| "no-floating-decimal": 1, | ||
| "no-multi-spaces": 1, | ||
| "object-property-newline": 1, | ||
| "padded-blocks": [1, "never"], | ||
| "space-before-blocks": 1, | ||
| "space-in-parens": 1, | ||
| "spaced-comment": 1, | ||
| "quotes": [1, "single"], | ||
| "id-length": [1, { "exceptions": ["i", "j", "x"] }], | ||
| "indent": [1, 2], | ||
| "no-array-constructor": 1 | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| g | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // Cifra un texto mediante el cifrado César. | ||
| function cipher() { | ||
| // Se obtiene el texto a cifrar. | ||
|
|
||
| var phrase = prompt('Escribe el texto a cifrar'); | ||
|
|
||
| // phrase es convertida a masyúscula y en un array con caracteres separados. | ||
|
|
||
| var phraseToArray = phrase.toUpperCase().split(''); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. que tal si dejamos que la cifra mantenga minusculas como minusculas y mayusculas como mayusculas? |
||
|
|
||
| // El array vacio, será nuevo array con los caracteres cifrados | ||
| newArray = []; | ||
| var number; | ||
| var numberToLetter; | ||
| var returnDecipher; | ||
|
|
||
| // Ciclo que itera por cada elemento del array phraseToArray. | ||
| for (var i = 0; i < phraseToArray.length; i++) { | ||
| // Si el elemento es un string vacio aparece nuevo prompt. | ||
| // Si el elemento parseado es >= a cero aparece nuevo prompt. | ||
| if (phrase[i] === ' ' || parseInt(phraseToArray[i]) >= 0) { | ||
| var text = 'El texto no debe contener espacios ni números, \n escribe el texto a cifrar.'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. El texto si debe de poder tener espacios! |
||
| phraseToArray = prompt(text).toUpperCase().split(''); | ||
|
|
||
| // El valor de i debe regresar a cero para volver a iterar por cada caracter. | ||
| i = 0; | ||
| } | ||
| } | ||
|
|
||
| // Ciclo que itera por cada elemento del array phraseToArray. | ||
| for (var j = 0; j < phraseToArray.length; j++) { | ||
| // Obtiene el valor ASCII de caracter que remplaza al elemento. | ||
| number = ((phraseToArray[j].charCodeAt(0) - 65) + 33 % 26 + 65); | ||
|
|
||
| // Si es mayor a 90 se disminuirá en 26 para solo obtener caracteres desde la A a la Z. | ||
| if (number > 90) { | ||
| number -= 26; | ||
| } | ||
|
|
||
| // Convierte en caracter según valor numérico. | ||
| numberToLetter = String.fromCharCode(number); | ||
|
|
||
| // Agrega el caracter al array. | ||
| newArray.push(numberToLetter); | ||
| } | ||
|
|
||
| // Muestra el texto cifrado en un string. | ||
| alert('Su texto cifrado es: ' + newArray.join('')); | ||
|
|
||
| // Da opción a descifrar el texto cifrado. | ||
| returnDecipher = confirm('¿Desea descifrar el texto cifrado?'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. el function de cifrar solo deberia de cifrar. Deberiamos poner toda la logica de descifrar en la otra function. |
||
|
|
||
| // Si acepta, se muestra texto original. | ||
| if (returnDecipher) { | ||
| alert('Su texto original es: ' + phrase); | ||
| } | ||
| } | ||
|
|
||
| cipher(); | ||
|
|
||
| // Descifra un texto codificado por el cifrado César. | ||
| function decipher() { | ||
| // Se obtiene el texto a cifrar. | ||
| var phrase = prompt('Escribe el texto a descifrar'); | ||
|
|
||
| // phrase es convertida a masyúscula y en un array con caracteres separados. | ||
| var phraseToArray = phrase.toUpperCase().split(''); | ||
|
|
||
| // El array vacio, será nuevo array con los caracteres cifrados | ||
| newArray = []; | ||
| var number; | ||
| var numberToLetter; | ||
| var returnDecipher; | ||
|
|
||
| // Ciclo que itera por cada elemento del array phraseToArray. | ||
| for (var i = 0; i < phraseToArray.length; i++) { | ||
| // Si el elemento es un string vacio aparece nuevo prompt. | ||
| // Si el elemento parseado es mayor a cero aparece nuevo prompt. | ||
| if (phrase[i] === ' ' || parseInt(phraseToArray[i]) >= 0) { | ||
| var text2 = 'El texto no debe contener espacios ni números, \n escribe la frase a cifrar.'; | ||
| phraseToArray = prompt(text2).toUpperCase().split(''); | ||
|
|
||
| // El valor de i debe regresar a cero para volver a iterar por cada elemento. | ||
| i = 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no me gusta cambiar el valor de los indices de for loops dentro de los for loops. Tal vez podrias hacer un helper function para verificar el input y hacer un while loop. Algo como: |
||
| } | ||
| } | ||
|
|
||
| // Ciclo que itera por cada elemento del array phraseToArray. | ||
| for (var j = 0; j < phraseToArray.length; j++) { | ||
| // Obtiene el valor ASCII de caracter que remplaza al elemento. | ||
|
|
||
| number = ((phraseToArray[j].charCodeAt(0) - 65) - 33 % 26 + 65); | ||
|
|
||
| // Si es menor a 65 se suma 26 para solo obtener caracteres desde la A a la Z. | ||
|
|
||
| if (number < 65) { | ||
| number = number + 26; | ||
| } | ||
|
|
||
| // Convierte en caracter según valor numérico. | ||
|
|
||
| numberToLetter = String.fromCharCode(number); | ||
|
|
||
| // Agrega el caracter al array. | ||
| newArray.push(numberToLetter); | ||
| } | ||
|
|
||
| // Muestra el texto descifrado en un string. | ||
| alert('Su texto cifrado es: ' + newArray.join('')); | ||
|
|
||
| // Da opción a cifrar el texto descifrado. | ||
|
|
||
| returnDecipher = confirm('¿Desea descifrar el texto cifrado?'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No deberias de tener que preguntar si desea descifrar el texto, porque ya estamos en la function para descifrar. |
||
|
|
||
| // Si acepta, se muestra texto cifrado. | ||
| if (returnDecipher) { | ||
| alert('Su texto original es: ' + phrase); | ||
| } | ||
| } | ||
|
|
||
| decipher(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no deberiamos de llamar decipher y cipher a menos que el usuario quiera hacerlo. tal vez podrias hacer un menu donde el usuario puede poner 1 para cifrar o 2 para descifrar |
||
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 pagina index.html deberia de tener contenido e importar el javascript usando <script>