Playground - Crea una cola de emails
Clase 92 de 99 • 30 días de JavaScript
Contenido del curso
Clase 92 de 99 • 30 días de JavaScript
Contenido del curso
Alejandro Anaya
David Ochoa
Joan Alexander Valerio Rodríguez
Fernando Marcial Rondón Puma
Jacobo Burbano
Angel Javier Sanchez Tenjo
Rubén Hernández Hernández
Carina Payleman
Victor Ortiz
Elias Rayas Gonzalez
Andres Ricardo Martinez Diaz
Gabriel Luna
JAIME EDUARDO DIAZ TOBON
Victor Hernandez
Alfonso Neil Jiménez Casallas
Leonardo de los angeles Espinoza Hernandez
David Esteban Giraldo Duque
Andres Eduardo Maneiro Antunez
Harold Zurita Simon
Alexis Corrales
Raul Carrillo Garrido
🛡️🛡️Escudo anti-spoilers🛡️🛡️
Mi solución al reto:
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { let newMail = new Mail(from, to, body, subject) if (this.length == 0) { this.first = newMail this.last = newMail } else { this.last.next = newMail this.last = newMail } this.length++ } dequeue() { let r = this.first; if (this.first === this.last) this.last = null; this.first = this.first.next; this.length-- delete r.next return r; } peek() { if (!this.first) return null return { from: this.first.from, to: this.first.to, body: this.first.body, subject: this.first.subject } } size() { return this.length } }
🛡️🛡️🛡️Escudo anti spoilers🛡️🛡️🛡️
Crea una cola de emails
!Spoiler Shield
Con ansias de que se venga el día 30 y tambien 30 días de Python 😍
Nunca pares de aprender 🦾
import { Node } from "./node"; export class Playlist { constructor() { this.top = null; this.bottom = null; this.length = 0; } addSong(song) { const newSong = new Node(song); if (!this.top) { this.top = newSong this.bottom = newSong console.log(this); } else { newSong.next = this.top this.top = newSong } this.length++; return this } playSong() { if (!this.top) { throw new Error("No hay canciones en la playlist") } if (this.top == this.bottom) { this.bottom = null } const playedSong = this.top.value this.top = this.top.next this.length--; return playedSong } getPlaylist() { const playList = [] if (!this.top) { return [] } if (this.top) { playList.push(this.top.value) console.log(this.top.value) let currentSong = this.top while (currentSong.next) { playList.push(currentSong.next.value) currentSong = currentSong.next } } return playList } }
Mi solución al problema. . . . . . . . . . . . . . .
mail.js
export class Mail { constructor(from, to, body, subject) { this.from = from; this.to = to; this.body = body; this.subject = subject; this.next = null; } }
exercise.js
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const newNode = new Mail(from, to, body, subject); if (this.length === 0) { this.first = newNode; this.last = newNode; } else { this.last.next = newNode; this.last = newNode; } this.length++; } dequeue() { if (this.length === 0) { throw new Error("La queue está vacía"); } const removedNode = this.first; if (this.first === this.last) { this.last = null; } this.first = this.first.next; this.length--; return { from: removedNode.from, to: removedNode.to, body: removedNode.body, subject: removedNode.subject, }; } peek() { if (this.length === 0) { throw new Error("La queue está vacía"); } let currentNode = this.first; currentNode.next = null; return currentNode; } size() { return this.length; } }
Creo que hay un error por parte de la eliminación del email ya que al desesctructurar el email mas viejo para quitar la clave que contiene al siguiente nodo/email retorna error, y funciona si simplemente se retorna el email mas antiguo junto con la clave de el email siguiente Mi solución al reto: . . . . . . . . . . .
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const newNode = new Mail(from, to, body, subject) if (this.length === 0) { this.last = newNode this.first = newNode } this.last.next = newNode this.last = newNode return this.length++ } dequeue() { if (!this.first) { throw new Error('No hay elementos en la cola') } if (this.first === this.last) { this.last = null } const deleteEmail = this.first this.first = this.first.next this.length-- return deleteEmail } peek() { const oldEmail = this.first delete oldEmail.next return oldEmail } size() { return this.length } }
Hola dejo mi solución:
✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅
Exercise.js
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const newMail = new Mail(from, to, body, subject); if (this.isEmpty()) { this.first = newMail; } else { this.last.next = newMail; } this.last = newMail; this.length++; } dequeue() { if (this.isEmpty()) { throw new Error("La cola está vacía"); } const removedMail = this.first; if (this.length === 1) { this.first = null; this.last = null; } else { this.first = removedMail.next; } removedMail.next = null; this.length--; return removedMail; } peek() { if (this.isEmpty()) { throw new Error("La cola está vacía"); } return this.first; } isEmpty() { return this.length === 0; } size() { return this.length; } }
💚Mi Solución💚
🛡️Escudo Anti-Spoilers🛡️
!cat ##👾Código👾
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const newMail = new Mail(from, to, body, subject) if (this.length == 0) { this.first = newMail this.last = newMail } else { this.last.next = newMail this.last = newMail } this.length++ } dequeue() { if (this.length == 0) throw new Error("No hay más mails") const mail = this.first this.first = this.first.next if (this.length-- == 1) this.last = null return mail } peek() { return this.first } size() { return this.length } }
exercise.js
class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const newMail = new Mail(from, to, body, subject); if (this.length === 0) { this.first = newMail; this.last = newMail; } else { this.last.next = newMail; this.last = newMail; } this.length++; } dequeue() { if (this.length === 0) throw new Error("La queue está vacía"); if (this.first === this.last) this.last = null; const deletedEmail = this.first; this.first = this.first.next; this.length--; return deletedEmail; } peek() { return this.first; } size() { return this.length; } }
Mi solucion: . . . . . . . . . . . . . . . . . . . . . .
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { // Tu código aquí 👈🏻 const newMail = new Mail(from, to, body, subject) // if (!this.first) Alternativa if (this.length === 0) { this.first = newMail; this.last = newMail; } else { this.last.next = newMail; this.last = newMail; } this.length++; } dequeue() { // Tu código aquí 👈🏻 if (this.length === 0) throw new Error('No hay email a revisar'); const { from, to, body, subject } = this.first; if (this.first === this.last) this.last = null; this.first = this.first.next; this.length--; return { from, to, body, subject }; } peek() { // Tu código aquí 👈🏻 if (this.length === 0) throw new Error('No hay email'); return this.first; } size() { // Tu código aquí 👈🏻 return this.length; } }
Solución
class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { // Tu código aquí 👈🏻 const newMail = new Mail(from, to, body, subject); if (this.length === 0) { this.first = newMail; this.last = newMail; } else { this.last.next = newMail; this.last = newMail; } this.length++; return this; } dequeue() { // Tu código aquí 👈🏻 const deletedMail = { from: this.first.from, to: this.first.to, body: this.first.body, subject: this.first.subject, }; if (this.first === this.last) { this.first = null; this.last = null; } else { this.first = this.first.next; } this.length--; return deletedMail; } peek() { // Tu código aquí 👈🏻 return this.first ? this.first : null; } size() { // Tu código aquí 👈🏻 return this.length ? this.length : "Empty mailbox"; } }
🛡️🛡️Escudo anti-spoilers🛡️🛡️ !penguin
Mi solución al reto
export class Mail { constructor(from, to, body, subject) { this.from = from; this.to = to; this.body = body; this.subject = subject; this.next = null; } json() { let { from, to, body, subject } = this; return { from, to, body, subject }; } }
export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const newMail = new Mail(from, to, body, subject); if (this.length === 0) { this.first = newMail; this.last = newMail; } else { this.last.next = newMail; this.last = newMail; } this.length++; } dequeue() { if (this.length === 0) { throw new Error("La queue está vacía"); } const removedMail = this.first; if (this.first === this.last) { this.last = null; } this.first = this.first.next; this.length--; return removedMail.json(); } peek() { return this.first.json(); } size() { return this.length; } }
Aquí mi solución: . . . . . . . . . . . . exercise.js
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const newNode = new Mail(from, to, body, subject); if (this.length === 0) { this.first = newNode; this.last = newNode; } else { this.last.next = newNode; this.last = newNode; } this.length++; } dequeue() { if (this.length === 0) { throw new Error("No hay correos en cola") } let { from, to, body, subject } = this.first; let removedNode = { from, to, body, subject }; if (this.first === this.last) { this.last = null; } // Reasignamos los valores this.first = this.first.next; // y reducimos la longitud this.length--; // Se retorna el valor del nodo removido return removedNode; } peek() { if (this.length === 0) { throw new Error("No hay correos en cola") } let { from, to, body, subject } = this.first; let firstNode = { from, to, body, subject }; return firstNode; } size() { return this.length; } }
++MI SOLUCION++ 💪 . . . . . . . . . . . . . . . . . . class Mail
export class Mail { constructor(from, to, body, subject) { this.from = from; this.to = to; this.body = body; this.subject = subject; this.next = null; } }
class Queue
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const newMail = new Mail(from, to, body, subject); if (this.length === 0) { this.first = newMail; this.last = newMail; } else { this.last.next = newMail; this.last = newMail; } this.length++; } dequeue() { if (this.length === 0) throw new Error("Los mails están vacíos"); const removedMail = { from: this.first.from, to: this.first.to, body: this.first.body, subject: this.first.subject }; if (this.first === this.last) this.last = null; this.first = this.first.next; this.length--; return removedMail; } peek() { return { from: this.first.from, to: this.first.to, body: this.first.body, subject: this.first.subject }; } size() { return this.length; } }
mail.js
export class Mail { constructor(from, to, body, subject) { this.from = from; this.to = to; this.body = body; this.subject = subject; this.next = null; } get() { return {from: this.from, to: this.to, body: this.body, subject: this.subject} } }
exercise.js
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const node = new Mail(from, to, body, subject); if (this.length === 0) { this.first = node; this.last = node; } else { this.last.next = node; this.last = node; } this.length++; } dequeue() { if (this.length === 0) { throw new Error("La cola está vacía"); } const node = this.first; if (this.first === this.last) { this.last = null; } this.first = this.first.next; this.length--; return node.get(); } peek() { if (this.length === 0) { throw new Error("La cola está vacía"); } return this.first.get(); } size() { return this.length; } }
estoy muy decepcionado con este ejercicio, porque el enunciado no corresponde con las pruebas unitarias, tuve que ver la solución para verificar, por favor, revisen para hacer los cambios correspondientes
¡Hola Alfonso!
¿Qué parte del enunciado te genera esta confusión? Para poder corregirlo lo más pronto posible
Mi solución: . . . . . . . . mail.js
export class Mail { constructor(from, to, body, subject) { this.from = from; this.to = to; this.body = body; this.subject = subject; this.next = null; } }
exercise.js
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const newEmail = new Mail(from, to, body, subject) if (this.length === 0) { this.first = newEmail this.last = newEmail } else { this.last.next = newEmail this.last = newEmail } this.length++ } dequeue() { if (this.length === 0) { throw new Error("La lista de correos está vacía") } const {next, ...removedEmail} = this.first if (this.first === this.last) { this.last = null } this.first = this.first.next this.length-- return removedEmail } peek() { const { next, ...firstEmail } = this.first return firstEmail } size() { return this.length } }
Mi humilde solucion . . . . . . . . . . . . . .
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const newEmail = new Mail(from, to, body, subject) if (!this.length) { this.first = newEmail this.last = newEmail } else { this.last.next = newEmail this.last = newEmail } this.length++ } dequeue() { if (!this.length) { throw new Error('La queue esta vacia') } let removedMail = this.first if (this.first === this.last) { this.last = null } this.first = this.first.next this.length-- const { from, to, body, subject } = removedMail return { from, to, body, subject } } peek() { if (!this.first) return null const { from, to, body, subject } = this.first return { from, to, body, subject } } size() { return this.length } }
Solución… 😄 . . . .
import { Mail } from "./mail"; export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { let newMail = new Mail(from, to, body, subject); if (!this.first) { this.first = newMail; this.last = newMail; } else { this.last.next = newMail; this.last = newMail; } this.length++; } dequeue() { if (!this.first) { throw new Error("No hay correos por revisar"); } let removedMail = this.first; if (this.first === this.last) { this.last = null; } this.first = this.first.next; this.length--; return removedMail.toObject(); } peek() { if (!this.first) { return null; } return this.first.toObject(); } size() { return this.length; } }
. mail.js:
export class Mail { constructor(from, to, body, subject) { this.from = from; this.to = to; this.body = body; this.subject = subject; this.next = null; } toObject() { return { from: this.from, to: this.to, body: this.body, subject: this.subject } } }
export class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } enqueue(from, to, body, subject) { const newMail = new Mail(from, to, body, subject); if (!this.first) { this.first = newMail; this.last = newMail; } else { this.last.next = newMail; this.last = newMail; } this.length++; } dequeue() { if (!this.first) { throw new Error("No hay elementos en la Queue"); } const removedMail = this.first; if (this.first === this.last) { this.last = null; } this.first = removedMail.next; this.length--; removedMail.next = null; return { from: removedMail.from, to: removedMail.to, body: removedMail.body, subject: removedMail.subject, }; } peek() { if (!this.first) { return null; } return { from: this.first.from, to: this.first.to, body: this.first.body, subject: this.first.subject, }; } size() { return this.length; } }
. . . . . ..
import { Mail } from "./mail"; export class Queue { constructor() { this.items = []; } enqueue(from, to, body, subject) { const email = new Mail(from, to, body, subject); this.items.push(email); } dequeue() { if (this.isEmpty()) { throw new Error("No hay elementos en la Queue"); } return this.items.shift().getData(); } peek() { if (this.isEmpty()) { return null; } return this.items[0].getData(); } size() { return this.items.length; } isEmpty() { return this.size() === 0; } }
export class Mail { constructor(from, to, body, subject) { this.from = from; this.to = to; this.body = body; this.subject = subject; this.timestamp = new Date().getTime(); } getData() { return { from: this.from, to: this.to, body: this.body, subject: this.subject, }; } }