Skip to content

Commit

Permalink
Merge pull request #5874 from RicJDev/main
Browse files Browse the repository at this point in the history
#34 y 35 - JavaScript
  • Loading branch information
Roswell468 committed Aug 31, 2024
2 parents 2fd7537 + e82ed3e commit 33b7262
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 5 deletions.
30 changes: 29 additions & 1 deletion Roadmap/09 - HERENCIA/typescript/RicJDev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ class Employee {
}
}

class Manager extends Employee {
interface hasWorkers {
workers: Employee[]
addWorker: (worker: Employee) => void
displayWorkersList: () => void
}

class Manager extends Employee implements hasWorkers {
workers: Employee[] = []

constructor(name: string, workerID: number) {
Expand All @@ -78,3 +84,25 @@ class Manager extends Employee {
})
}
}

class ProjectManager extends Employee implements hasWorkers {
workers: Employee[]

constructor(name: string, workerID: number) {
super(name, workerID, 'Project Manager')
}

addWorker(worker: Employee) {
this.workers.push(worker)
}

displayWorkersList(): void {
console.log(`${this.name}'s workers:`)

this.workers.forEach((worker) => {
console.log(`- ${worker.name}: ${worker.workerID}. ${worker.title}`)
})
}
}

class Programer extends Employee {}
15 changes: 11 additions & 4 deletions Roadmap/32 - BATALLA DEADPOOL Y WOLVERINE/javascript/RicJDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ const rl = readline.createInterface({

//Modelado de personajes
class Character {
constructor(name, hp, attackRange = { min: 0, max: 50 }, defenseRate) {
/**
* @param {string} name
* @param {number} hp
* @param {number} defenseRate
* @param {{ min: number, max: number, }} attackRange
*/

constructor(name, hp, defenseRate, attackRange) {
this.name = name
this.hp = this.validateHp(hp)

this.attackRange = attackRange
this.defenseRate = defenseRate
this.attackRange = attackRange

this.canAttack = true
}
Expand Down Expand Up @@ -140,11 +147,11 @@ async function main() {

//Deadpool
let deadpoolHP = parseInt(await rl.question('Indique la cantidad de vida para Deadpool. '))
const Deadpool = new Character(pc.red('Deadpool'), deadpoolHP, { min: 10, max: 100 }, 25)
const Deadpool = new Character(pc.red('Deadpool'), deadpoolHP, 25, { min: 10, max: 100 })

//Wolverine
let wolverineHP = parseInt(await rl.question('Indique la cantidad de vida para Wolverine. '))
const Wolverine = new Character(pc.yellow('Wolverine'), wolverineHP, { min: 10, max: 120 }, 20)
const Wolverine = new Character(pc.yellow('Wolverine'), wolverineHP, 20, { min: 10, max: 120 })

console.log(pc.gray('Cargando...'))
simulateBattle(Deadpool, Wolverine)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
EJERCICIO
@RicJDev
*/

class Person {
constructor(id, name) {
this.name = name
this.id = id

this.parents = []
this.children = []

this.partner = null
}

addChild(child) {
if (!this.children.includes(child)) {
if (child.hasParents()) {
console.log(
`${child.name} ya tiene padres: ${child.parents[0].name}, ${child.parents[1].name}.`
)
} else {
child.parents.push(this)
this.children.push(child)

console.log(`${this.name} ha tenido un hijo: ${child.name}.`)
}
} else {
console.log(`${child.name} ya es hijo de ${this.name}.`)
}
}

setPartner(partner) {
if (partner.hasPartner() || this.hasPartner()) {
this.hasPartner()
? console.log(`${this.name} ya tiene pareja: ${this.partner.name}.`)
: console.log(`${partner.name} ya tiene pareja: ${partner.partner.name}.`)
} else {
this.partner = partner
partner.partner = this

console.log(`${this.name} ahora es pareja de ${partner.name}.`)
}
}

hasPartner() {
return !(this.partner === null)
}

hasParents() {
return this.parents.length === 2
}
}

class FamilyTree {
constructor() {
this.people = {}
}

addPerson(id, name) {
if (this.people[id]) {
console.log(`El ID ${id} ya ha sido registrado.`)
} else {
this.people[id] = new Person(id, name)

console.log(`Se ha registrado a ${name} [ID: ${id}].`)
}
}

deletePerson(id) {
this.people[id]
? delete this.people[id]
: console.log(`No se ha encontrado a ninguna persona con la ID: ${id || 0}.`)
}

setPartner(id1, id2) {
const person1 = this.people[id1]
const person2 = this.people[id2]

person1 && person2
? person1.setPartner(person2)
: console.log(`Una de las ID's no coincide con ningún registro: ${id1 || 0}, ${id2 || 0}`)
}

addChild(id, childId) {
const parent = this.people[id]
const child = this.people[childId]

if (parent && child) {
parent.addChild(child)

if (parent.hasPartner()) {
parent.partner.addChild(child)
}
} else {
console.log(`Una de las ID's no coincide con ningún registro: ${id || 0}, ${childId || 0}`)
}
}

displayTree() {
const visited = new Set()

function printPerson(person, level) {
if (visited.has(person.id)) return
visited.add(person.id)

let ident = ' '.repeat(level * 4)

console.log(`${ident} - ${person.name} [ID: ${person.id}]`)

if (person.partner) {
visited.add(person.partner.id)

console.log(`${ident} Pareja: ${person.partner.name} [ID: ${person.id}]`)
}

if (person.children.length > 0) {
console.log(`${ident} Hijos:`)
person.children.forEach((child) => {
printPerson(child, level + 1)
})
}
}

const rootPersons = Object.values(this.people).filter((person) => {
return person.parents.length === 0
})

rootPersons.forEach((root) => printPerson(root, 0))
}
}

const tree = new FamilyTree()

tree.addPerson(1, 'Aegon I Targaryen')
tree.addPerson(2, 'Rhaenys Targaryen')
tree.addPerson(3, 'Visenya Targaryen')

tree.addPerson(4, 'Aenys I Targaryen')
tree.addPerson(5, 'Maegor I Targaryen')

tree.addPerson(6, 'Jaehaerys I Targaryen')
tree.addPerson(7, 'Alysanne Targaryen')

tree.addPerson(8, 'Aegon II Targaryen')
tree.addPerson(9, 'Helaena Targaryen')

tree.addPerson(10, 'Viserys II Targaryen')

tree.addPerson(11, 'Rhaenyra Targaryen')
tree.addPerson(12, 'Daemon Targaryen')

tree.setPartner(1, 2)
tree.setPartner(1, 3)
tree.setPartner(4, 7)
tree.setPartner(8, 9)
tree.setPartner(11, 12)

tree.addChild(1, 4)
tree.addChild(1, 5)

tree.addChild(4, 6)
tree.addChild(4, 11)

tree.addChild(6, 8)
tree.addChild(6, 10)

console.log(' ')
tree.displayTree()
100 changes: 100 additions & 0 deletions Roadmap/35 - REPARTIENDO LOS ANILLOS DE PODER/javascript/RicJDev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
EJERCICIO:
- Se ejecuta en Node.js, importando el módulo 'Readline/promises' (https://nodejs.org/api/readline.html)
@RicJDev
*/

function isPrime(num) {
if (num <= 1) return false
if (num <= 3) return true
if (num % 2 === 0 || num % 3 === 0) return false

for (let i = 5; i * i <= num; i += 6) {
if (num % i === 0 || num % (i + 2) === 0) {
return false
}
}

return true
}

const isEven = (num) => num % 2 === 0

function distributeRings(totalRings) {
let rings = null

if (totalRings < 6) {
console.warn('No hay suficientes anillos para repartir')
return rings
}

totalRings -= 1

let bestDiference = Number.MAX_SAFE_INTEGER

for (let elvesRings = 1; elvesRings <= totalRings; elvesRings += 2) {
for (let dwarvesRings = 2; dwarvesRings <= totalRings; dwarvesRings++) {
if (isPrime(dwarvesRings)) {
let humansRings = totalRings - elvesRings - dwarvesRings

if (isEven(humansRings) && humansRings > 0) {
const diference =
Math.max(elvesRings, dwarvesRings, humansRings) -
Math.min(elvesRings, dwarvesRings, humansRings)

if (diference < bestDiference) {
bestDiference = diference

rings = {
elves: elvesRings,
dwarves: dwarvesRings,
humans: humansRings,
sauron: 1,
}
}
}
}
}
}

if (rings === null) {
console.warn('No se ha encontrado una combinación para repartir los anillos')
}

return rings
}

//Implementación en terminal

import * as readline from 'readline/promises'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})

console.clear()

while (true) {
let answer = await rl.question('Indique la cantidad de anillos a repartir. ')

let totalRings = parseInt(answer.match(/\d+/g))

if (isNaN(totalRings)) {
console.log('Por favor igresar numeros enteros')
} else {
const rings = distributeRings(totalRings)

if (rings !== null) {
console.log(`\nSe han distribuido los ${totalRings} anillos de la siguiente manera: `)
console.log('Elfos:', rings.elves)
console.log('Enanos:', rings.dwarves)
console.log('Hombres:', rings.humans)
console.log('Sauron:', rings.sauron)

rl.close()
break
}
}
}

0 comments on commit 33b7262

Please sign in to comment.