Skip to content

Commit 2f508b2

Browse files
committed
Add search method to the extra challenge
1 parent 174a081 commit 2f508b2

File tree

1 file changed

+136
-4
lines changed
  • Roadmap/03 - ESTRUCTURAS DE DATOS/javascript

1 file changed

+136
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// DATA STRUCTURES
22

3-
// ARRAYS
3+
console.log("----- ARRAYS -----");
44
const fruitsArr = ["Banana", "Pear", "Apple", "Apricot", "Banana"];
55
console.log("Fruits:", fruitsArr, "\n");
66

@@ -10,13 +10,16 @@ console.log("Array length:", fruitsArr.length);
1010
console.log("fruitsArr[2] =>", fruitsArr[2]);
1111
console.log("fruitsArr[3] =>", fruitsArr[3]);
1212

13-
console.log("\nMétodos de los Arrays:")
14-
13+
console.log("\nMétodos de los Arrays:");
1514

1615
// Push -- Añadir (Modifica el array original)
17-
fruitsArr.push(['Pear', 'Apple']);
16+
fruitsArr.push(["Pear", "Apple"]);
1817
console.log("- Push =>", fruitsArr);
1918

19+
// Unshift
20+
fruitsArr.unshift("Watermelon");
21+
console.log("- Unshift =>", fruitsArr);
22+
2023
// Filter
2124
const bananasArr = fruitsArr.filter((fruit) => fruit === "Banana");
2225
console.log("- Filter =>", bananasArr);
@@ -25,9 +28,138 @@ console.log("- Filter =>", bananasArr);
2528
const foundFruit = fruitsArr.find((fruit) => fruit.startsWith("Apr"));
2629
console.log("- Find =>", foundFruit);
2730

31+
// Sort
32+
const numbers = [2, 3, 10, 2, 4];
33+
console.log(
34+
"- Sort =>",
35+
numbers.sort((a, b) => a - b)
36+
);
37+
38+
// Splice
39+
console.log("- Splice =>", fruitsArr.splice(1, fruitsArr.length));
40+
2841
// Reverse
2942
console.log("- Reverse =>", fruitsArr.reverse());
3043

3144
// Flat
3245
console.log("- Flat =>", fruitsArr.flat());
3346

47+
console.log("----- OBJECTS -----");
48+
const person = {
49+
id: 15,
50+
name: "Marcos",
51+
age: 20,
52+
knowledge: {
53+
javascript: false,
54+
go: false,
55+
},
56+
};
57+
console.log("Person: ", person);
58+
59+
person.name = "Lucas";
60+
person.knowledge = { ...person.knowledge, python: true };
61+
62+
console.log("\nModified Person Obj: ", person);
63+
64+
console.log("----- MAPS -----");
65+
// The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.
66+
67+
const people = new Map();
68+
people.set("Poncho", { phone: "2347651029" });
69+
people.set("Vicky", { phone: "2347654449", address: "9 Fow Ave" });
70+
71+
console.log(people);
72+
73+
// Extra Challenge
74+
// Agenda de Contactos
75+
import * as readline from "node:readline/promises";
76+
import { stdin as input, stdout as output } from "node:process";
77+
import { SERVFAIL } from "node:dns";
78+
const rl = readline.createInterface({ input, output });
79+
80+
const contacts = [
81+
{ id: 1, name: "Juan", phone: 1234560987 },
82+
{ id: 1, name: "Pedro", phone: 1234560987 },
83+
];
84+
85+
const ops = [
86+
{ id: 1, name: "Search" },
87+
{ id: 2, name: "Add" },
88+
{ id: 3, name: "Update" },
89+
{ id: 4, name: "Delete" },
90+
];
91+
92+
const searchContact = async () => {
93+
console.log(`
94+
*****************************************************
95+
* SEARCH CONTACT *
96+
*****************************************************
97+
`);
98+
const contactInput = await rl.question("Contact name? ");
99+
100+
const contact = contacts.find((contact) => {
101+
return contact.name === contactInput;
102+
});
103+
104+
if (!contact) {
105+
console.log("Contact not found, please try again");
106+
searchContact();
107+
} else {
108+
console.log(contact);
109+
}
110+
111+
agenda();
112+
};
113+
114+
const addContact = () => {
115+
console.log("Add");
116+
};
117+
const updateContact = () => {
118+
console.log("Update");
119+
};
120+
const deleteContact = () => {
121+
console.log("Delete");
122+
};
123+
124+
async function agenda() {
125+
const op = await rl.question(
126+
`
127+
*****************************************************
128+
* AGENDA *
129+
*****************************************************
130+
* *
131+
* What do you want to do today? *
132+
* *
133+
* 1: Search Contact *
134+
* 2: Add Contact *
135+
* 3: Update Contact *
136+
* 4: Delete Contact *
137+
* *
138+
* 0: Exit program *
139+
*****************************************************
140+
141+
* Insert the operation number or 0 to exit: `
142+
);
143+
switch (+op) {
144+
case 1:
145+
searchContact();
146+
break;
147+
case 2:
148+
addContact();
149+
break;
150+
case 3:
151+
updateContact();
152+
break;
153+
case 4:
154+
deleteContact();
155+
break;
156+
case 0:
157+
rl.close();
158+
break;
159+
default:
160+
console.log("Operation not valid");
161+
break;
162+
}
163+
}
164+
165+
agenda();

0 commit comments

Comments
 (0)