import{Mail}from"./mail";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){let newMail =newMail(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.nextreturn r;}peek(){if(!this.first)returnnullreturn{from:this.first.from,to:this.first.to,body:this.first.body,subject:this.first.subject}}size(){returnthis.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";exportclassPlaylist{constructor(){this.top=null;this.bottom=null;this.length=0;}addSong(song){const newSong =newNode(song);if(!this.top){this.top= newSong
this.bottom= newSong
console.log(this);}else{ newSong.next=this.topthis.top= newSong
}this.length++;returnthis}playSong(){if(!this.top){thrownewError("No hay canciones en la playlist")}if(this.top==this.bottom){this.bottom=null}const playedSong =this.top.valuethis.top=this.top.nextthis.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.topwhile(currentSong.next){ playList.push(currentSong.next.value) currentSong = currentSong.next}}return playList
}}
Mi solución al problema.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
mail.js
exportclassMail{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";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const newNode =newMail(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){thrownewError("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){thrownewError("La queue está vacía");}let currentNode =this.first; currentNode.next=null;return currentNode;}size(){returnthis.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";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const newNode =newMail(from, to, body, subject)if(this.length===0){this.last= newNode
this.first= newNode
}this.last.next= newNode
this.last= newNode
returnthis.length++}dequeue(){if(!this.first){thrownewError('No hay elementos en la cola')}if(this.first===this.last){this.last=null}const deleteEmail =this.firstthis.first=this.first.nextthis.length--return deleteEmail
}peek(){const oldEmail =this.firstdelete oldEmail.nextreturn oldEmail
}size(){returnthis.length}}
Hola dejo mi solución:
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
✅
Exercise.js
import{Mail}from"./mail";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const newMail =newMail(from, to, body, subject);if(this.isEmpty()){this.first= newMail;}else{this.last.next= newMail;}this.last= newMail;this.length++;}dequeue(){if(this.isEmpty()){thrownewError("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()){thrownewError("La cola está vacía");}returnthis.first;}isEmpty(){returnthis.length===0;}size(){returnthis.length;}}
💚Mi Solución💚
🛡️Escudo Anti-Spoilers🛡️
!cat
##👾Código👾
import{Mail}from"./mail";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const newMail =newMail(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)thrownewError("No hay más mails")const mail =this.firstthis.first=this.first.nextif(this.length--==1)this.last=nullreturn mail
}peek(){returnthis.first}size(){returnthis.length}}
.
.
.
.
.
.
exercise.js
classQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const newMail =newMail(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)thrownewError("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(){returnthis.first;}size(){returnthis.length;}}
import{Mail}from"./mail";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){// Tu código aquí 👈🏻const newMail =newMail(from, to, body, subject)// if (!this.first) Alternativaif(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)thrownewError('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)thrownewError('No hay email');returnthis.first;}size(){// Tu código aquí 👈🏻returnthis.length;}}
Solución
classQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){// Tu código aquí 👈🏻const newMail =newMail(from, to, body, subject);if(this.length===0){this.first= newMail;this.last= newMail;}else{this.last.next= newMail;this.last= newMail;}this.length++;returnthis;}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í 👈🏻returnthis.first?this.first:null;}size(){// Tu código aquí 👈🏻returnthis.length?this.length:"Empty mailbox";}}
🛡️🛡️Escudo anti-spoilers🛡️🛡️
!penguin
Mi solución al reto
exportclassMail{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 };}}
exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const newMail =newMail(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){thrownewError("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(){returnthis.first.json();}size(){returnthis.length;}}
import{Mail}from"./mail";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const newNode =newMail(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){thrownewError("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 valoresthis.first=this.first.next;// y reducimos la longitudthis.length--;// Se retorna el valor del nodo removidoreturn removedNode;}peek(){if(this.length===0){thrownewError("No hay correos en cola")}let{from, to, body, subject }=this.first;let firstNode ={from, to, body, subject };return firstNode;}size(){returnthis.length;}}
exportclassMail{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";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const newMail =newMail(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)thrownewError("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(){returnthis.length;}}
mail.js
exportclassMail{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";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const node =newMail(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){thrownewError("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){thrownewError("La cola está vacía");}returnthis.first.get();}size(){returnthis.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
exportclassMail{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";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const newEmail =newMail(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){thrownewError("La lista de correos está vacía")}const{next,...removedEmail}=this.firstif(this.first===this.last){this.last=null}this.first=this.first.nextthis.length--return removedEmail
}peek(){const{ next,...firstEmail }=this.firstreturn firstEmail
}size(){returnthis.length}}
Mi humilde solucion
.
.
.
.
.
.
.
.
.
.
.
.
.
.
import{Mail}from"./mail";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const newEmail =newMail(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){thrownewError('La queue esta vacia')}let removedMail =this.firstif(this.first===this.last){this.last=null}this.first=this.first.nextthis.length--const{from, to, body, subject }= removedMail
return{from, to, body, subject }}peek(){if(!this.first)returnnullconst{from, to, body, subject }=this.firstreturn{from, to, body, subject }}size(){returnthis.length}}
Solución… 😄
.
.
.
.
.
.
exercise.js:
import{Mail}from"./mail";exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){let newMail =newMail(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){thrownewError("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){returnnull;}returnthis.first.toObject();}size(){returnthis.length;}}
.
mail.js:
exportclassMail{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}}}
exportclassQueue{constructor(){this.first=null;this.last=null;this.length=0;}enqueue(from, to, body, subject){const newMail =newMail(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){thrownewError("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){returnnull;}return{from:this.first.from,to:this.first.to,body:this.first.body,subject:this.first.subject,};}size(){returnthis.length;}}
.
.
.
.
.
..
import{Mail}from"./mail";exportclassQueue{constructor(){this.items=[];}enqueue(from, to, body, subject){const email =newMail(from, to, body, subject);this.items.push(email);}dequeue(){if(this.isEmpty()){thrownewError("No hay elementos en la Queue");}returnthis.items.shift().getData();}peek(){if(this.isEmpty()){returnnull;}returnthis.items[0].getData();}size(){returnthis.items.length;}isEmpty(){returnthis.size()===0;}}
exportclassMail{constructor(from, to, body, subject){this.from=from;this.to= to;this.body= body;this.subject= subject;this.timestamp=newDate().getTime();}getData(){return{from:this.from,to:this.to,body:this.body,subject:this.subject,};}}