Skip to content

Commit 4453130

Browse files
authored
Merge pull request mouredev#7599 from 1Nonamed/solutions-nonamed
#3 - JavaScript
2 parents 228f804 + 2f508b2 commit 4453130

File tree

1 file changed

+165
-0
lines changed
  • Roadmap/03 - ESTRUCTURAS DE DATOS/javascript

1 file changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// DATA STRUCTURES
2+
3+
console.log("----- ARRAYS -----");
4+
const fruitsArr = ["Banana", "Pear", "Apple", "Apricot", "Banana"];
5+
console.log("Fruits:", fruitsArr, "\n");
6+
7+
console.log("Array length:", fruitsArr.length);
8+
9+
// Acceder a las posiciones de un Array
10+
console.log("fruitsArr[2] =>", fruitsArr[2]);
11+
console.log("fruitsArr[3] =>", fruitsArr[3]);
12+
13+
console.log("\nMétodos de los Arrays:");
14+
15+
// Push -- Añadir (Modifica el array original)
16+
fruitsArr.push(["Pear", "Apple"]);
17+
console.log("- Push =>", fruitsArr);
18+
19+
// Unshift
20+
fruitsArr.unshift("Watermelon");
21+
console.log("- Unshift =>", fruitsArr);
22+
23+
// Filter
24+
const bananasArr = fruitsArr.filter((fruit) => fruit === "Banana");
25+
console.log("- Filter =>", bananasArr);
26+
27+
// Find
28+
const foundFruit = fruitsArr.find((fruit) => fruit.startsWith("Apr"));
29+
console.log("- Find =>", foundFruit);
30+
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+
41+
// Reverse
42+
console.log("- Reverse =>", fruitsArr.reverse());
43+
44+
// Flat
45+
console.log("- Flat =>", fruitsArr.flat());
46+
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)