-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7582 from victor-Casta/victorcasta@gmail.com
#34 - JavaScript
- Loading branch information
Showing
4 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
Roadmap/16 - EXPRESIONES REGULARES/typescript/victor-Casta.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const text: string = 'lor0em 9ip6sum 2 dolor 334, asit am3ed' | ||
const getNumbersRegex: RegExp = /\d/g | ||
console.log(text.match(getNumbersRegex)?.join('')) | ||
|
||
// extra | ||
|
||
const emailRegex: RegExp = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ | ||
const phoneRegex: RegExp = /^\+?[1-9]\d{1,14}$/ | ||
const urlRegex: RegExp = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// 1. for | ||
for(let i:number = 1; i <= 10; i++) { | ||
console.log(i) | ||
} | ||
|
||
// 2. while | ||
let i:number = 1 | ||
while (i <= 10) { | ||
console.log(i) | ||
i++ | ||
} | ||
|
||
// 3. do while | ||
let j: number = 1 | ||
do { | ||
console.log(j) | ||
j++ | ||
} while (j <= 10) | ||
|
||
// extra | ||
|
||
|
||
const numbers: number[] = [1, 2, 3, 4, 5] | ||
|
||
// 4. for of | ||
console.log("4. for...of") | ||
for (const number of numbers) { | ||
console.log(number) | ||
} | ||
|
||
// 5. for in | ||
console.log("5. for...in") | ||
for (const index in numbers) { | ||
console.log(numbers[index]) | ||
} | ||
|
||
// 6. forEach | ||
console.log("6. forEach") | ||
numbers.forEach((number) => { | ||
console.log(number) | ||
}); | ||
|
||
// 7. map | ||
console.log("7. map") | ||
numbers.map((number) => { | ||
console.log(number) | ||
}); | ||
|
||
// 8. filter | ||
console.log("8. filter") | ||
numbers.filter((number) => { | ||
console.log(number) | ||
return true | ||
}); | ||
|
||
// 9. reduce | ||
console.log("9. reduce"); | ||
numbers.reduce((_, number) => { | ||
console.log(number) | ||
return 0 | ||
}, 0) | ||
|
||
// 10. every | ||
console.log("10. every"); | ||
numbers.every((number) => { | ||
console.log(number) | ||
return true | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const myList: (number | number[])[] = [1,2,3,4] | ||
|
||
myList.push(5) | ||
myList.unshift(0) | ||
myList.push([10, 11, 12]) | ||
myList[2] = [20, 21, 23] | ||
myList.splice(0, 1) | ||
myList[0] = 3 | ||
myList.includes(3) | ||
myList.length = 0 | ||
|
||
/* | ||
Extra | ||
*/ | ||
|
||
const mySet1: Set<number> = new Set([1, 3, 5, 7, 9]) | ||
const mySet2: Set<number> = new Set([1, 4, 9]) | ||
|
||
// Unión | ||
console.log(mySet1.union(mySet2)) | ||
|
||
// Intersección | ||
console.log(mySet1.intersection(mySet2)) | ||
|
||
// Diferencia | ||
console.log(mySet1.difference(mySet2)) | ||
|
||
// Diferencia simétrica | ||
console.log(mySet1.symmetricDifference(mySet2)) | ||
|
||
/* | ||
* Referencia: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set | ||
*/ |
106 changes: 106 additions & 0 deletions
106
Roadmap/34 - ÁRBOL GENEALÓGICO LA CASA DEL DRAGÓN/javascript/victor-Casta.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
class Person { | ||
constructor(id, name) { | ||
this.id = id | ||
this.name = name | ||
this.partner = null | ||
this.children = [] | ||
} | ||
} | ||
|
||
class FamilyTree { | ||
constructor() { | ||
this.people = new Map() | ||
} | ||
|
||
addPerson(id, name) { | ||
if (this.people.has(id)) { | ||
console.log(`La persona con ID ${id} ya existe.`) | ||
return | ||
} | ||
const newPerson = new Person(id, name) | ||
this.people.set(id, newPerson) | ||
console.log(`Persona ${name} añadida al árbol.`) | ||
} | ||
|
||
removePerson(id) { | ||
if (!this.people.has(id)) { | ||
console.log(`La persona con ID ${id} no existe.`) | ||
return | ||
} | ||
|
||
const person = this.people.get(id) | ||
if (person.partner) { | ||
person.partner.partner = null | ||
} | ||
|
||
this.people.forEach((p) => { | ||
p.children = p.children.filter((child) => child.id !== id) | ||
}) | ||
|
||
this.people.delete(id) | ||
console.log(`Persona con ID ${id} eliminada del árbol.`) | ||
} | ||
|
||
setPartner(id1, id2) { | ||
if (!this.people.has(id1) || !this.people.has(id2)) { | ||
console.log(`Una o ambas personas no existen.`) | ||
return | ||
} | ||
|
||
const person1 = this.people.get(id1) | ||
const person2 = this.people.get(id2) | ||
|
||
if (person1.partner || person2.partner) { | ||
console.log(`Una de las personas ya tiene pareja.`) | ||
return | ||
} | ||
|
||
person1.partner = person2 | ||
person2.partner = person1 | ||
console.log(`${person1.name} y ${person2.name} ahora son pareja.`) | ||
} | ||
|
||
addChild(parentId, childId) { | ||
if (!this.people.has(parentId) || !this.people.has(childId)) { | ||
console.log(`Una o ambas personas no existen.`) | ||
return | ||
} | ||
|
||
const parent = this.people.get(parentId) | ||
const child = this.people.get(childId) | ||
|
||
if (!parent.children.includes(child)) { | ||
parent.children.push(child) | ||
console.log(`${child.name} añadido como hijo/a de ${parent.name}.`) | ||
} else { | ||
console.log(`${child.name} ya es hijo/a de ${parent.name}.`) | ||
} | ||
} | ||
|
||
printTree() { | ||
this.people.forEach((person) => { | ||
console.log(`ID: ${person.id}, Nombre: ${person.name}`) | ||
if (person.partner) { | ||
console.log(` Pareja: ${person.partner.name}`) | ||
} | ||
if (person.children.length > 0) { | ||
const childNames = person.children.map((child) => child.name).join(", ") | ||
console.log(` Hijos: ${childNames}`) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
const tree = new FamilyTree() | ||
tree.addPerson(1, "Alicent") | ||
tree.addPerson(2, "Viserys") | ||
tree.addPerson(3, "Rhaenyra") | ||
tree.addPerson(4, "Daemon") | ||
|
||
tree.setPartner(1, 2) | ||
tree.addChild(1, 3) | ||
tree.setPartner(3, 4) | ||
|
||
tree.printTree() | ||
tree.removePerson(3) | ||
tree.printTree() |