Skip to content

Commit 08449a6

Browse files
committed
#3 - javascript
1 parent a701827 commit 08449a6

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// ESTRUCTURAS SOPORTADAS DE JS
2+
3+
const { clear } = require('console');
4+
const { read } = require('fs');
5+
6+
// Primitivos
7+
let number = 12;
8+
let string = 'Hola mundo!';
9+
let boolean = true;
10+
let indefinito = undefined;
11+
let isNull = null;
12+
13+
// Objetos
14+
let objeto1 = {
15+
nombre: 'Jeronimo',
16+
edad: 22,
17+
lenguaje: 'javascript',
18+
};
19+
objeto1["sexo"] = 'hombre'; // insercion
20+
delete objeto1.lenguaje; // borrado
21+
objeto1.edad = 20 // actualizacion
22+
console.log(objeto1)
23+
24+
let arreglo1 = [1, 2, 3, 4];
25+
let arreglo2 = ['a', 'b', 'c', 'd'];
26+
arreglo1.push(7); // insercion
27+
arreglo2.pop(); // borrado
28+
arreglo1[0] = 0; // actualizado
29+
arreglo1.reverse(); // ordenado
30+
31+
console.log(arreglo1);
32+
console.log(arreglo2);
33+
34+
function unaFuncion(){
35+
console.log('Soy una funcion');
36+
}
37+
38+
let fecha = new Date();
39+
40+
// Otros
41+
let promesa = new Promise((res, rej) => {
42+
// operacion
43+
})
44+
45+
async function funcionAsync(){
46+
let variable = await promesa//....
47+
};
48+
49+
50+
51+
// EXTRA
52+
let contactos = [
53+
{nombre: 'Jeronimo', numero: 123456788},
54+
]
55+
56+
const readline = require('readline').createInterface({
57+
input: process.stdin,
58+
output: process.stdout,
59+
})
60+
// Busqueda de contacto
61+
function busqueda(name){
62+
let encontrado = false;
63+
for(let i of contactos){
64+
if(i.nombre === name){
65+
console.log(`\nEl número telefonico de ${name} es: ${i.numero}`);
66+
encontrado = true;
67+
}
68+
}
69+
if(!encontrado) console.log(`\n${name} no esta en nuestra agenda`);
70+
espera();
71+
}
72+
// Insercion de contacto
73+
function insercion(newName, newNumber){
74+
let encontrado = false;
75+
for(let i of contactos){
76+
if(i.numero === newNumber){
77+
console.log('\nEste numero ya esta agendado, debe haber un error..');
78+
encontrado = true;
79+
}
80+
}
81+
if(newNumber.toString().length >= 11 || isNaN(newNumber)){
82+
console.log('\nDebe ser numerico y menor a 11 digitos de largo..')
83+
}
84+
if(!encontrado && !isNaN(newNumber) && newNumber.toString().length < 11) {
85+
contactos.push({nombre: newName, numero: newNumber});
86+
console.log(`\nContacto agregado: ${newName} - ${newNumber}`);
87+
}
88+
espera();
89+
}
90+
function actualizacion(toUpdate, newName, newNumber){
91+
let encontrado = false;
92+
for(let i of contactos){
93+
if(i.nombre === toUpdate){
94+
i.nombre = newName;
95+
i.numero = newNumber;
96+
encontrado = true;
97+
break;
98+
}
99+
}
100+
if(!encontrado) console.log(`\n${toUpdate} no está en nuestra agenda`);
101+
if(newNumber.toString().length >= 11 || isNaN(newNumber)) console.log('\nDebe ser numerico y menor a 11 digitos de largo..');
102+
espera();
103+
}
104+
function eliminar(conctact){
105+
let incialLength = contactos.length;
106+
contactos = contactos.filter(persona => persona.nombre !== conctact);
107+
if(incialLength > contactos.length){
108+
console.log(`\n${conctact} se ha eliminado con exito!`);
109+
} else{
110+
console.log(`No se encontro ${conctact} en nuestros contactos`)
111+
}
112+
espera();
113+
}
114+
function espera(){
115+
readline.question('\nPrecione ENTER para continuar..', (tecla) =>{
116+
if(tecla === ''){
117+
console.clear();
118+
agenda()
119+
} else{
120+
espera();
121+
}
122+
})
123+
}
124+
function makeNumber(str){
125+
let number = parseInt(str);
126+
return number;
127+
}
128+
function makeName(name){
129+
let nameMod = name[0].toUpperCase() + name.slice(1).toLowerCase();
130+
return nameMod;
131+
}
132+
133+
function agenda(){
134+
console.log(`
135+
======= Agenda Telefonica ======= \n
136+
1. Buscar contacto
137+
2. Agregar contacto
138+
3. Actulizar contacto
139+
4. Borrar contacto
140+
0. Salir \n`);
141+
142+
readline.question('Elija una opcion: ', (opcion) => {
143+
opcionNumber = parseInt(opcion);
144+
switch (opcionNumber){
145+
case 1:
146+
readline.question('Ingrese el nombre de la persona que quiere buscar: ', (nombreBuscado) =>{
147+
busqueda(makeName(nombreBuscado));
148+
})
149+
break;
150+
case 2:
151+
readline.question('Ingrese el nombre del contacto que quiere agregar: ', (newNameContact) => {
152+
readline.question('Ahora ingrese el número del nuevo contacto: ', (newNumContact) => {
153+
insercion(makeName(newNameContact), makeNumber(newNumContact));
154+
})
155+
})
156+
break;
157+
case 3:
158+
readline.question('Ingrese el nombre del contacto que quiere actualizar: ', (contactToUpdate) => {
159+
readline.question('Ingrese el nuevo nombre del contacto: ', (nameUpdate) =>{
160+
readline.question('Ingrese el nuevo número del contacto: ', (numberUpdate) => {
161+
actualizacion(makeName(contactToUpdate), makeName(nameUpdate), makeNumber(numberUpdate));
162+
})
163+
})
164+
})
165+
break;
166+
case 4:
167+
readline.question('Ingrese el nombre del contacto que desea eliminar: ', (contactToDelete) =>{
168+
eliminar(makeName(contactToDelete));
169+
})
170+
break;
171+
case 0:
172+
console.log('\nSaliendo..')
173+
readline.close();
174+
break;
175+
default:
176+
console.log(`"${opcionNumber}" no es una opcion valida`);
177+
espera();
178+
}
179+
})
180+
}
181+
agenda()

0 commit comments

Comments
 (0)