Playground - Proxy en servicio de mensajería
Clase 74 de 99 • 30 días de JavaScript
Contenido del curso
Clase 74 de 99 • 30 días de JavaScript
Contenido del curso
Andres Eduardo Maneiro Antunez
Carina Payleman
Andres Felipe Diago Matta
Joan Alexander Valerio Rodríguez
Daniel Romao
Rubén Hernández Hernández
David Ochoa
Alexis Corrales
Angel Javier Sanchez Tenjo
Elias Rayas Gonzalez
Raul Carrillo Garrido
Victor Ortiz
JAIME EDUARDO DIAZ TOBON
Israel Blas
Israel Blas
Jose Barboza
Andrés Camilo Vélez Mejía
Santiago Andres Alvarez Cuadros
Alejandro Anaya
Gabriel Luna
Abril Darynka Tapia Sosa
Mi Solucion . . . . . . . . . . . . . . . . . ..
user.js
export class User { constructor(name) { this.name = name this.isLogged = false } login() { this.isLogged = true } logout() { this.isLogged = false } isLoggedIn() { return this.isLogged } }
exercies.js
export class MessagesProxy { constructor(messages, user) { this.messages = messages, this.user = user } validateUserLogin(user) { if (!user) { throw new Error("Usuario no registrado") } else { return true } } sendMessage(text) { this.validateUserLogin(this.user.isLoggedIn()) && this.messages.sendMessage(text) } getHistory() { return this.validateUserLogin(this.user.isLoggedIn()) && this.messages.getHistory() } }
messages.js
export class Messages { constructor() { this.history = [] } sendMessage(text) { this.history.push(text) } getHistory() { return this.history } }
class MessagesProxy { constructor(messages, user) { this.messages = messages; this.user = user; } sendMessage(text) { if (this.user.isLoggedIn()) { return this.messages.sendMessage(text); } else { throw new Error("Usuario no registrado"); } } getHistory() { if (this.user.isLoggedIn()) { return this.messages.history; } else { throw new Error("Usuario no registrado"); } } }
Solucion: . . . . . . . . . . . . .
class Messages { // No debes editar este código ❌ constructor() { this.history = []; } sendMessage(text) { this.history.push(text); } getHistory() { return this.history; } } class User { constructor(name) { // Tu código aquí 👈 this.name = name; this._isLoggedIn = false; } login() { // Tu código aquí 👈 this._isLoggedIn = true; } logout() { // Tu código aquí 👈 if(this._isLoggedIn) { this._isLoggedIn = false; } } isLoggedIn() { // Tu código aquí 👈 return this._isLoggedIn; } } class MessagesProxy { constructor(messages, user) { // Tu código aquí 👈 this.messages = messages; this.user = user; } sendMessage(text) { if(this.user.isLoggedIn()) { this.messages.sendMessage(text); }else { throw new Error("Usuario no registrado"); } } getHistory() { if(this.user.isLoggedIn()) { return this.messages.getHistory(); }else { throw new Error("Usuario no registrado"); } } } //Ejemplo 1: //Input: const user = new User("John") user.login() console.log(user.isLoggedIn()) //Output: true //Ejemplo 2: //Input: const user2 = new User("John") const messages = new Messages() const messagesProxy = new MessagesProxy(messages, user2) user2.login() messagesProxy.sendMessage("Hola") console.log(messagesProxy.getHistory()) //Output: ["Hola"] //Ejemplo 3: //Input: const user3 = new User("John") console.log(user3.isLoggedIn()) //Output: false const messages3 = new Messages() const messagesProxy3 = new MessagesProxy(messages3, user3) messagesProxy3.sendMessage("Hola") //Output: Error("Usuario no registrado")
De mejorar un poco mas la redacción del ejercicio, se haría a la primera. Esta sencillo 🤓
Mi solucion: . . . . . . . . . . . User.js
export class User { constructor(name) { this.name = name; this.isLogged = null; } login() { this.isLogged = true; } logout() { this.isLogged = false; } isLoggedIn() { return this.isLogged; } }
exercise.js
export class MessagesProxy { constructor(messages, user) { this.messages = messages; this.user = user; } sendMessage(text) { if (!this.user.isLoggedIn()) throw new Error('Usuario no registrado'); this.messages.sendMessage(text); } getHistory() { if (!this.user.isLoggedIn()) throw new Error('Usuario no registrado'); return this.messages.getHistory(); } }
💚 Mi Solución 💚
🛡️MURO ANTI SPOILERS🛡️
!car
User.js
export class User { constructor(name) { this.name = name this._isLogged = false } login() { this._isLogged = true } logout() { this._isLogged = false } isLoggedIn() { return this._isLogged } }
exercise.js
export class MessagesProxy { constructor(messages, user) { this.messages = messages, this.user = user } sendMessage(text) { if (!this.user.isLoggedIn()) throw new Error("Usuario no registrado") this.messages.sendMessage(text) } getHistory() { if (!this.user.isLoggedIn()) throw new Error("Usuario no registrado") return this.messages.getHistory() } }
🛡️🛡️🛡️Escudo anti spoilers🛡️🛡️🛡️
Proxy en servicio de mensajeria
!Spoiler Shield
exercise.js
export class MessagesProxy { constructor(messages, user) { this.messages = messages; this.user = user; } sendMessage(text) { if (this.user.isLogged) { this.messages.sendMessage(text); } else { throw new Error("Usuario no registrado"); } } getHistory() { if (this.user.isLogged) { return this.messages.getHistory(); } else { throw new Error("Usuario no registrado"); } } }
User.js
export class User { constructor(name) { this.name = name; this.isLogged = false; } login() { this.isLogged = true; } logout() { this.isLogged = false; } isLoggedIn() { return this.isLogged; } }
User
export class User { constructor(name) { this.name = name; this.loggedIn = false; } login() { this.loggedIn = true; } logout() { this.loggedIn = false; } isLoggedIn() { return this.loggedIn; } }
exercise
export class MessagesProxy { constructor(messages, user) { this.messages = messages; this.user = user; } sendMessage(text) { if (!this.user || !this.user.isLoggedIn()) { throw new Error("Usuario no registrado"); } this.messages.sendMessage(text); } getHistory() { if (!this.user || !this.user.isLoggedIn()) { throw new Error("Usuario no registrado"); } return this.messages.getHistory(); } }
Hola mi solución al problema ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅
User.js
export class User { constructor(name) { this.name = name; this.loggedIn = false; } login() { this.loggedIn = true; } logout() { this.loggedIn = false; } isLoggedIn() { return this.loggedIn; } }
exercise.js
export class MessagesProxy { constructor(messages, user) { this.messages = messages; this.user = user; } sendMessage(text) { if (this.user.isLoggedIn()) { this.messages.sendMessage(text); } else { throw new Error("Usuario no logueado"); } } getHistory() { if (this.user.isLoggedIn()) { return this.messages.getHistory(); } else { throw new Error("Usuario no logueado"); } } }
messages.js
export class Messages { // No debes editar este código ❌ constructor() { this.history = []; } sendMessage(text) { this.history.push(text); } getHistory() { return this.history; } }
Solucion + + + + + + + + +
class User { constructor(name) { // Tu código aquí 👈 this.name = name; this.loggedIn = false; } login() { // Tu código aquí 👈 if (!this.isLoggedIn()) { this.loggedIn = true; } } logout() { // Tu código aquí 👈 if (this.isLoggedIn()) { this.loggedIn = false; } } isLoggedIn() { // Tu código aquí 👈 return this.loggedIn; } } class MessagesProxy { constructor(messages, user) { // Tu código aquí 👈 this.messages = messages; this.user = user; } sendMessage(text) { // Tu código aquí 👈 if (this.user.isLoggedIn()) { this.messages.sendMessage(text); } else { throw new Error("User not registered"); } } getHistory() { // Tu código aquí 👈 if (this.user.isLoggedIn()) { return this.messages.getHistory(); } } }
. . . . . . . . . . . .
export class User { constructor(name) { this.name = name; this.loggedIn = false; } login() { this.loggedIn = true; } logout() { this.loggedIn = false; } isLoggedIn() { return this.loggedIn; } }
.
export class MessagesProxy { constructor(messages, user) { this.messages = messages; this.user = user; } sendMessage(text) { if (this.user.isLoggedIn()) { return this.messages.sendMessage(text); } else { throw new Error("Usuario no registrado"); } } getHistory() { if (this.user.isLoggedIn()) { return this.messages.getHistory(); } else { throw new Error("Usuario no registrado"); } } }
Bien, esta hecho, pero todavía me quedan dudas: . . . . . . . . . . . . . . . . . . .
exercise.js
export class MessagesProxy { constructor(messages, user) { // Tu código aquí 👈 this.messages = messages; this.user = user; } send() { if (!this.user.isLog) throw new Error('Usuario no registrado'); return true; } sendMessage(text) { // Tu código aquí 👈 if (this.send()) this.messages.sendMessage(text); } getHistory() { // Tu código aquí 👈 if (this.send()) return this.messages.getHistory(); } }
user.js
export class User { constructor(name) { // Tu código aquí 👈 this.name = name; this.isLog = false; } login() { // Tu código aquí 👈 this.isLog = true; } logout() { // Tu código aquí 👈 this.isLog = false; } isLoggedIn() { // Tu código aquí 👈 return this.isLog; } }
++MI SOLUCION++ 💪 . . . . . . . . . . . . . . . . class User
export class User { constructor(name) { this.name = name; this.isLogin = false; } login() { this.isLogin = true; } logout() { this.isLogin = false; } isLoggedIn() { return this.isLogin; } }
class MessagesProxy
export class MessagesProxy { constructor(messages, user) { this.messages = messages; this.user = user; } sendMessage(text) { if (!this.user.isLoggedIn()) throw new Error("Usuario no registrado"); this.messages.sendMessage(text); } getHistory() { if (!this.user.isLoggedIn()) throw new Error("Usuario no registrado"); return this.messages.getHistory(); } }
Un poco raro pero me corrío el segundo test recien cuando puse el condicional tambien en gethistory
Bueno ahí va mi solución:
. . . . . . . . . .
export class MessagesProxy { constructor(messages, user) { this.messages = messages this.user = user } sendMessage(text) { if (!this.user.isLoggedIn()) { throw new Error("Usuario no registrado") } this.messages.sendMessage(text) } getHistory() { if (!this.user.isLoggedIn()) { throw new Error("Usuario no registrado") } return this.messages.getHistory() } }
Y en User le puse esto:
export class User { constructor(name) { this.name = name this.status = false } login() { this.status = true } logout() { this.status = false } isLoggedIn() { return this.status } }
code!!
export class MessagesProxy { constructor(messages, user) { this.messages = messages; this.user = user; } sendMessage(text) { if (this.user.isLoggedIn()) { this.messages.sendMessage(text); } else { throw new Error("Usuario no registrado") } } getHistory() { return this.messages.getHistory(); } }
User.js
export class User { constructor(name) { // Tu código aquí 👈 this.name = name; this._isLoggedIn = false; } login() { // Tu código aquí 👈 this._isLoggedIn = true; } logout() { // Tu código aquí 👈 this._isLoggedIn = false; } isLoggedIn() { // Tu código aquí 👈 return this._isLoggedIn; } }
exercise.js
export class MessagesProxy { constructor(messages, user) { // Tu código aquí 👈 this.messages = messages; this.user = user; } sendMessage(text) { // Tu código aquí 👈 if (!this.user.isLoggedIn()) { throw new Error("Usuario no registrado"); } this.messages.sendMessage(text); } getHistory() { // Tu código aquí 👈 if (!this.user.isLoggedIn()) { throw new Error("Usuario no registrado") } return this.messages.getHistory(); } }
Mi solución: . . . . . . . . . .
User.js
export class User { #isLogged constructor(name) { this.name = name this.#isLogged = false } login() { this.#isLogged = true } logout() { this.#isLogged = false } isLoggedIn() { return this.#isLogged } }
excercise.js
export class MessagesProxy { constructor(messages, user) { this.messages = messages this.user = user } #isUserLoggedIn() { if (!this.user.isLoggedIn()) { throw new Error("Usuario no registrado") } return true } sendMessage(text) { if (this.#isUserLoggedIn()) this.messages.sendMessage(text) } getHistory() { if (this.#isUserLoggedIn()) return this.messages.getHistory() } }
🛡️🛡️Escudo anti-spoilers🛡️🛡️
Mi solución al reto:
export class User { constructor(name) { this.name = name this.status = false } login() { this.status = true } logout() { this.status = false } isLoggedIn() { return this.status } }
exercise.js
export class MessagesProxy { constructor(messages, user) { this.messages = messages this.user = user } sendMessage(text) { if (!this.user.isLoggedIn()) throw new Error("Usuario no registrado") this.messages.sendMessage(text) } getHistory() { if (!this.user.isLoggedIn()) throw new Error("Usuario no registrado") return this.messages.getHistory(); } }
Aquí mi solución: . . . . . . . . . .
User.js
export class User { constructor(name) { this._name = name this._isLoggedIn = false } login() { this._isLoggedIn = true; } logout() { this._isLoggedIn = false; } isLoggedIn() { return this._isLoggedIn; } }
exercise.js
export class MessagesProxy { constructor(messages, user) { this.messages = messages; this.user = user; } sendMessage(text) { if (this.verifyLogIn()) { this.messages.sendMessage(text); } } getHistory() { if (this.verifyLogIn()) { return this.messages.getHistory() } } verifyLogIn() { if (this.user.isLoggedIn()) { return true; } else { throw new Error("Usuario no registrado"); } } }
Hola, dejo mi solución ✨ . . . . . . . . . . . . .
exercise.js
export class MessagesProxy { constructor(messages, user) { // Tu código aquí 👈 this.messages = messages; this.user = user; } sendMessage(text) { // Tu código aquí 👈 if (this.user.loggedIn) { this.messages.sendMessage(text); } else { throw new Error(`Usuario no registrado`) } } getHistory() { // Tu código aquí 👈 if (this.user.loggedIn) { return this.messages.getHistory(); } else { throw new Error(`Usuario no registrado`) } } }
user.js
export class User { constructor(name) { // Tu código aquí 👈 this.name = name; this.loggedIn = false; } login() { // Tu código aquí 👈 this.loggedIn = true; } logout() { // Tu código aquí 👈 this.loggedIn = false; } isLoggedIn() { // Tu código aquí 👈 return this.loggedIn; } }