-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
128 lines (107 loc) · 4.5 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Reglas de encriptación:
"e" es convertido para "enter"
"i" es convertido para "imes"
"a" es convertido para "ai"
"o" es convertido para "ober"
"u" es convertido para "ufat"
Solo letras minusculas
No se permite acentuación de palabras
*/
/* Reglas de desencriptación:
"enter" es convertido para "e"
"imes" es convertido para "i"
"ai" es convertido para "a"
"ober" es convertido para "o"
"ufat" es convertido para "u"
Solo letras minusculas
No se permite acentuación de palabras
*/
//--------------- MI EXPERIENCIA CON EL CHALLENGUE ---------------//
//Para el manejo del encriptado pense en una forma de poder recorrer el texto
//e ir remplazando vocal por vocal, entonces busque funciones que me ayudaran e
//implemente el siguiente metodo:
//----ENCRIPTAR----//
//-Guardar el texto en un array, letra por letra, para ello obtuve la posicion de cada indice del string gracias a la funcion charAt()
//-Luego recorrer el array e ir remplazando los datos, para ello use la funcion splice()
//-Use concat() para pasar todo el array encriptado a un string
//----DESENCRIPTAR----//
//-En la funcion desencriptar fue mas sencillo porque con la funcion replace cambie los datos correspondientes.
//----- Funcion para encriptar el texto -----//
function encriptarTexto(texto){
arrayTexto = [];
var largoTexto = texto.length;
//Guardamos el texto dentro del array
for(var indiceTexto = 0; indiceTexto < largoTexto; indiceTexto++){
arrayTexto.push(texto.charAt(indiceTexto));
}
var largoArray = arrayTexto.length;
//Recorrer el array para intercambiar el texto segun corresponda
for(var indiceArray = 0; indiceArray < largoArray; indiceArray++){
if(arrayTexto[indiceArray] == 'e'){
arrayTexto.splice(indiceArray, 1, 'enter');
}else if(arrayTexto[indiceArray] == 'i'){
arrayTexto.splice(indiceArray, 1, 'imes');
}else if(arrayTexto[indiceArray] == 'a'){
arrayTexto.splice(indiceArray, 1, 'ai');
}else if(arrayTexto[indiceArray] == 'o'){
arrayTexto.splice(indiceArray, 1, 'ober');
}else if(arrayTexto[indiceArray] == 'u'){
arrayTexto.splice(indiceArray, 1, 'ufat');
}
}
//Pasar los indices del array a un string
var textoEncriptado = "".concat(...arrayTexto);
//Imprimir el resultado en el input
console.log(textoEncriptado);
var textoSalida = document.querySelector("#msg");
textoSalida.value = textoEncriptado;
}
//----- Funcion para Desencriptar el texto -----//
function desencriptarTexto(texto){
//Remplazar los valores correspondientes
var textoDesencriptado = texto.replace(/ai/g,'a');
textoDesencriptado = textoDesencriptado.replace(/enter/g,'e');
textoDesencriptado = textoDesencriptado.replace(/imes/g,'i');
textoDesencriptado = textoDesencriptado.replace(/ober/g,'o');
textoDesencriptado = textoDesencriptado.replace(/ufat/g,'u');
//Imprimir el resultado en el input
console.log(textoDesencriptado);
var textoSalida = document.querySelector("#msg");
textoSalida.value = textoDesencriptado;
}
//----- Capturando los botones y pasandoles las funciones correspondientes -----//
var botonEncriptar = document.querySelector("#btn-encriptar");
botonEncriptar.addEventListener("click",function(event){
event.preventDefault();
var textoEntrada = document.querySelector("#input-texto").value;
encriptarTexto(textoEntrada);
});
var botonDesencriptar = document.querySelector("#btn-desencriptar");
botonDesencriptar.addEventListener("click",function(event){
event.preventDefault();
var textoEntrada = document.querySelector("#input-texto").value;
desencriptarTexto(textoEntrada);
});
//----- Funcion para copiar en el portapapeles -----//
function copy() {
var copyText = document.querySelector("#msg");
copyText.select();
document.execCommand("copy");
}
document.querySelector("#btn-copy").addEventListener("click", copy);
//----- Funcion para limpiar los inputs -----//
function limpiar(input){
input.value= "";
}
var botonLimpiarEntrada = document.querySelector("#btn-limpiarEntrada");
botonLimpiarEntrada.addEventListener("click",function(event){
event.preventDefault();
var textoEntrada = document.querySelector("#input-texto");
limpiar(textoEntrada);
});
var botonLimpiarSalida = document.querySelector("#btn-limpiarSalida");
botonLimpiarSalida.addEventListener("click",function(event){
event.preventDefault();
var textoSalida = document.querySelector("#msg");
limpiar(textoSalida);
});