|
| 1 | +// const { get } = require('http'); |
| 2 | +const readline = require('readline'); |
| 3 | + |
| 4 | +/* |
| 5 | +EJERCICIO: |
| 6 | +- Muestra ejemplos de creación de todas las estructuras soportadas por defecto en tu lenguaje. |
| 7 | +- Utiliza operaciones de inserción, borrado, actualización y ordenación. |
| 8 | +*/ |
| 9 | +// --- List --- |
| 10 | +// Population, update operations |
| 11 | +let fruitsList = ['Banana', 'Apple']; // Creating an array |
| 12 | +fruitsList[0] = 'Orange'; // Modifying an element based on its position |
| 13 | +fruitsList.push('Banana'); // Adding an element to the end |
| 14 | +fruitsList.unshift('Banana'); // Adding an element to the beginning |
| 15 | +fruitsList.splice(1, 0, 'Strawberry') // Adding an element on an specific position |
| 16 | +fruitsList.splice(4, 1, 'Pear') // Replacing an element on an specific position |
| 17 | +fruitsList.splice(0, 0, 'Grapes', 'Watermelon') // Adding more than one element starting on an specific position |
| 18 | +let groceryList = new Array(); // Creating an array |
| 19 | +groceryList = fruitsList.concat(['Tomato', 'Pepper']) // Adding another array to the first array |
| 20 | + |
| 21 | +// Removing operations |
| 22 | +groceryList = groceryList.slice(0,3); // Slicing the array |
| 23 | +groceryList.pop(); // Removing the last element |
| 24 | +groceryList.shift(); // Removing the first element |
| 25 | +groceryList.splice(1, 1); // Removing an element of an specific position |
| 26 | +groceryList.reverse(); // Reversing the array |
| 27 | + |
| 28 | +// Sorting operation |
| 29 | +let numberList = [10, 2, 3, 5, 73]; |
| 30 | +numberList.sort((a, b) => a - b); // Sorting the array ascending order |
| 31 | +numberList.sort((a, b) => b - a); // Sorting the array descending order |
| 32 | + |
| 33 | +// --- Maps --- |
| 34 | +const phoneCharacteristics = new Map(); // Creating a map |
| 35 | +phoneCharacteristics.set('Battery', '100'); // Adding an element to the map |
| 36 | +phoneCharacteristics.set('Portability', true); // Adding an element to the map |
| 37 | +phoneCharacteristics.set('Wireless', false); // Adding an element to the map |
| 38 | +phoneCharacteristics.set('PhoneNumber', 12321) // Adding an element to the map |
| 39 | +phoneCharacteristics.delete('Battery'); // Remove an element using it's key |
| 40 | +let phonePortability = phoneCharacteristics.get('Portability'); // Get an element vßalue using it's key |
| 41 | + |
| 42 | +// --- Set --- |
| 43 | +const user = new Set(); // Creating a set |
| 44 | +user.add('name'); // Adding an element to the set |
| 45 | +user.add('age'); // Adding an element to the set |
| 46 | +user.add('weight'); // Adding an element to the set |
| 47 | +user.delete('weight'); // Removing an element of the set |
| 48 | +user.clear(); // Clear all the set |
| 49 | + |
| 50 | + |
| 51 | +// --- Objects --- |
| 52 | +let newCar = {color: 'red', hasWindows: true, year: 2025}; // Creating an object |
| 53 | +newCar.electric = false; // Adding a new element to the object |
| 54 | +newCar.color = 'Blue'; // Updating a value based on a key |
| 55 | +delete newCar.hasWindows; // Removing an element |
| 56 | +isColorKeyAvailable = 'color' in newCar // Verifying that an object has a value |
| 57 | + |
| 58 | +/* |
| 59 | +DIFICULTAD EXTRA (opcional): |
| 60 | +Crea una agenda de contactos por terminal. |
| 61 | +- Debes implementar funcionalidades de búsqueda, inserción, actualización y eliminación de contactos. |
| 62 | +- Cada contacto debe tener un nombre y un número de teléfono. |
| 63 | +- El programa solicita en primer lugar cuál es la operación que se quiere realizar, y a continuación |
| 64 | +los datos necesarios para llevarla a cabo. |
| 65 | +- El programa no puede dejar introducir números de teléfono no númericos y con más de 11 dígitos. |
| 66 | +(o el número de dígitos que quieras) |
| 67 | +- También se debe proponer una operación de finalización del programa. |
| 68 | +*/ |
| 69 | +let agenda = []; |
| 70 | + |
| 71 | +const rl = readline.createInterface({ |
| 72 | + input: process.stdin, |
| 73 | + output: process.stdout, |
| 74 | +}); |
| 75 | + |
| 76 | +function getUserInput(questionText) { |
| 77 | + return new Promise((resolve) => { |
| 78 | + rl.question(questionText, (ans) => { |
| 79 | + resolve(ans); |
| 80 | + }); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +async function registerContact() { |
| 85 | + let contact = {}; |
| 86 | + const contactName = await getUserInput('Enter the contact name: '); |
| 87 | + const contactPhoneNumber = await getUserInput('Enter contact phone number: '); |
| 88 | + if (isNaN(contact.phoneNumber) || contact.phoneNumber.toString().split('').length > 11) { |
| 89 | + console.log('The entered value must be a number with less than 11 digits'); |
| 90 | + showMenu(); |
| 91 | + } |
| 92 | + else { |
| 93 | + contact = {name: contactName, phoneNumber: contactPhoneNumber}; |
| 94 | + agenda.push(contact); |
| 95 | + console.log('---------------------------------'); |
| 96 | + console.log('New entry registered successfully'); |
| 97 | + console.log('Contact name: ' + contact.name); |
| 98 | + console.log('Contact phone number: ' + contact.phoneNumber); |
| 99 | + console.log('---------------------------------'); |
| 100 | + showMenu(); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +async function updateContact(nameToSearch) { |
| 105 | + const contact = agenda.find(contact => contact.name === nameToSearch); |
| 106 | + if (contact) { |
| 107 | + contact.phoneNumber = await getUserInput('Enter new phone number: '); |
| 108 | + if (isNaN(contact.phoneNumber) || contact.phoneNumber.toString().split('').length > 11) { |
| 109 | + console.log('The entered value must be a number with less than 11 digits'); |
| 110 | + showMenu(); |
| 111 | + } |
| 112 | + else { |
| 113 | + console.log('---------------------------------'); |
| 114 | + console.log('New phone number registered successfully'); |
| 115 | + console.log('Contact name: ' + contact.name); |
| 116 | + console.log('Contact phone number: ' + contact.phoneNumber); |
| 117 | + console.log('---------------------------------'); |
| 118 | + showMenu(); |
| 119 | + } |
| 120 | + } |
| 121 | + else { |
| 122 | + contactNotFoundMessage(); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +async function removeContact(nameToSearch) { |
| 127 | + const contact = agenda.find(contact => contact.name === nameToSearch); |
| 128 | + if (contact) { |
| 129 | + console.log('---------------------------------'); |
| 130 | + console.log('The contact was succesfully removed from the agenda'); |
| 131 | + console.log('Contact name: ' + contact.name); |
| 132 | + console.log('Contact phone number: ' + contact.phoneNumber); |
| 133 | + const index = agenda.indexOf(contact); |
| 134 | + agenda.splice(index, 1); |
| 135 | + console.log('---------------------------------'); |
| 136 | + showMenu(); |
| 137 | + } |
| 138 | + else { |
| 139 | + contactNotFoundMessage(); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +async function getPhoneNumberByName(nameToSearch) { |
| 144 | + // Find the contact with the matching name in the global 'agenda' list |
| 145 | + const contact = agenda.find(contact => contact.name === nameToSearch); |
| 146 | + if (contact) { |
| 147 | + console.log('---------------------------------'); |
| 148 | + console.log('Contact "' + nameToSearch + '" found in the agenda'); |
| 149 | + console.log(nameToSearch + ': ' + contact.phoneNumber); |
| 150 | + console.log('---------------------------------'); |
| 151 | + showMenu() |
| 152 | + } |
| 153 | + else { |
| 154 | + contactNotFoundMessage(); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +function isAgendaEmpty() { |
| 159 | + if (agenda.length === 0) { |
| 160 | + console.log('---------------------------------'); |
| 161 | + console.log('Agenda is empty, try adding some contacts first'); |
| 162 | + console.log('---------------------------------'); |
| 163 | + showMenu(); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +function contactNotFoundMessage() { |
| 168 | + console.log('---------------------------------'); |
| 169 | + console.log('The contact was not found in the agenda'); |
| 170 | + console.log('---------------------------------'); |
| 171 | + showMenu(); |
| 172 | +} |
| 173 | + |
| 174 | +async function showMenu() { |
| 175 | + console.log('Select an option'); |
| 176 | + console.log('1. Register a new contact (Insert)'); |
| 177 | + console.log('2. Update an existing contact'); |
| 178 | + console.log('3. Remove an existing contact'); |
| 179 | + console.log('4. Search a contact') |
| 180 | + console.log('5. Exit'); |
| 181 | + const option = await getUserInput('Select your option (1-5): '); |
| 182 | + switch (option) { |
| 183 | + case '1': |
| 184 | + registerContact(); |
| 185 | + break; |
| 186 | + case '2': |
| 187 | + isAgendaEmpty(); |
| 188 | + const nameToUpdate = await getUserInput('Enter the contact name to update: '); |
| 189 | + updateContact(nameToUpdate); |
| 190 | + break; |
| 191 | + case '3': |
| 192 | + isAgendaEmpty(); |
| 193 | + const nameToRemove = await getUserInput('Enter the contact name to remove: '); |
| 194 | + removeContact(nameToRemove); |
| 195 | + break; |
| 196 | + case '4': |
| 197 | + isAgendaEmpty(); |
| 198 | + const nameToSearch = await getUserInput('Enter the contact name to search: '); |
| 199 | + getPhoneNumberByName(nameToSearch); |
| 200 | + break; |
| 201 | + case '5': |
| 202 | + process.exit(0); |
| 203 | + default: |
| 204 | + console.log('---------------------------------'); |
| 205 | + console.log('Invalid option. Please select a valid option (1-5)'); |
| 206 | + console.log('---------------------------------'); |
| 207 | + showMenu(); |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +showMenu(); |
0 commit comments