Skip to content

Commit edeb28b

Browse files
authored
Merge pull request mouredev#7614 from 1Nonamed/solutions-nonamed
#3 - JavaScript
2 parents 5c64b22 + cc67d7b commit edeb28b

File tree

1 file changed

+115
-16
lines changed
  • Roadmap/03 - ESTRUCTURAS DE DATOS/javascript

1 file changed

+115
-16
lines changed

Roadmap/03 - ESTRUCTURAS DE DATOS/javascript/1Nonamed.js

+115-16
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ const rl = readline.createInterface({ input, output });
7979

8080
const contacts = [
8181
{ id: 1, name: "Juan", phone: 1234560987 },
82-
{ id: 1, name: "Pedro", phone: 1234560987 },
82+
{ id: 1, name: "Juan", phone: 1222543388 },
83+
{ id: 2, name: "Pedro", phone: 1255566427 },
8384
];
8485

8586
const ops = [
@@ -89,34 +90,126 @@ const ops = [
8990
{ id: 4, name: "Delete" },
9091
];
9192

93+
const showAgenda = (arr) => {
94+
console.log(`
95+
*****************************************************
96+
* CONTACT LIST *
97+
*****************************************************`);
98+
arr.map((contact) =>
99+
console.log(` * Name: ${contact.name}
100+
* Phone: +57 ${contact.phone}
101+
-----------------------------------------------------`)
102+
);
103+
};
104+
105+
const isExit = async (op) => {
106+
const answer = await rl.question(`
107+
*****************************************************
108+
* *
109+
* Please select one of the following options: *
110+
* *
111+
* 1: Go back and try again *
112+
* 2: Go to the Main Menu *
113+
* 3: Show contact list and exit *
114+
* 0: Exit *
115+
* *
116+
*****************************************************
117+
* `);
118+
119+
if (+answer === 0) {
120+
rl.close();
121+
} else if (+answer === 1) {
122+
op();
123+
} else if (+answer === 2) {
124+
agenda();
125+
} else {
126+
showAgenda(contacts);
127+
rl.close();
128+
}
129+
};
130+
92131
const searchContact = async () => {
93132
console.log(`
94133
*****************************************************
95134
* SEARCH CONTACT *
96-
*****************************************************
97-
`);
98-
const contactInput = await rl.question("Contact name? ");
135+
*****************************************************`);
136+
const contactInput =
137+
await rl.question(` * Contact name? *
138+
* `);
99139

100-
const contact = contacts.find((contact) => {
101-
return contact.name === contactInput;
140+
const contactsFound = contacts.filter((contact) => {
141+
return contact.name.toLowerCase() === contactInput.toLowerCase();
102142
});
103143

104-
if (!contact) {
105-
console.log("Contact not found, please try again");
106-
searchContact();
144+
if (contactsFound.length === 0) {
145+
console.log(` * *
146+
* Contact not found. *`);
147+
isExit(searchContact);
107148
} else {
108-
console.log(contact);
149+
showAgenda(contactsFound);
150+
isExit(searchContact);
109151
}
110-
111-
agenda();
112152
};
113153

114-
const addContact = () => {
115-
console.log("Add");
154+
const addContact = async () => {
155+
console.log(`
156+
*****************************************************
157+
* ADD CONTACT *
158+
*****************************************************`);
159+
const contactInputName =
160+
await rl.question(` * Contact name? *
161+
* `);
162+
const contactInputPhone =
163+
await rl.question(` * *
164+
* Phone number? *
165+
* `);
166+
167+
const nameHasNumbers = /\d/.test(contactInputName);
168+
const phoneHasLetters = /[a-zA-Z]/g.test(contactInputPhone);
169+
170+
if (contactInputName === "" || nameHasNumbers) {
171+
console.log(`
172+
* Contact name is not valid. *
173+
* Only letters allowed. *`);
174+
isExit(addContact);
175+
} else if (
176+
contactInputPhone === "" ||
177+
phoneHasLetters ||
178+
contactInputPhone.length > 10
179+
) {
180+
console.log(`
181+
* Contact phone number is not valid. *
182+
* Only numbers allowed, has to be 10 digits *`);
183+
isExit(addContact);
184+
} else {
185+
const newContact = {
186+
id: contacts.length + 1,
187+
name: contactInputName,
188+
phone: +contactInputPhone,
189+
};
190+
191+
contacts.push(newContact);
192+
console.log(` *
193+
* The contact ${newContact.name} has been added sucessfully *`);
194+
isExit(addContact);
195+
}
116196
};
117-
const updateContact = () => {
118-
console.log("Update");
197+
198+
const updateContact = async () => {
199+
console.log(`
200+
*****************************************************
201+
* UPDATE CONTACT *
202+
*****************************************************`);
203+
const contactInputData =
204+
await rl.question(` * Contact to update? Type name or phone number
205+
* `);
206+
207+
const filteredContact = contacts.find((contact) => {
208+
contact.name === contactInputData || contact.phone === contactInputData;
209+
});
210+
console.log(` * Updating contact ${contactInputData}`);
119211
};
212+
120213
const deleteContact = () => {
121214
console.log("Delete");
122215
};
@@ -134,8 +227,10 @@ async function agenda() {
134227
* 2: Add Contact *
135228
* 3: Update Contact *
136229
* 4: Delete Contact *
230+
* 5: Show contact list *
137231
* *
138232
* 0: Exit program *
233+
* *
139234
*****************************************************
140235
141236
* Insert the operation number or 0 to exit: `
@@ -153,6 +248,10 @@ async function agenda() {
153248
case 4:
154249
deleteContact();
155250
break;
251+
case 5:
252+
showAgenda(contacts);
253+
isExit();
254+
break;
156255
case 0:
157256
rl.close();
158257
break;

0 commit comments

Comments
 (0)