¡Te damos la bienvenida a este reto!

1

¡Bienvenido al mundo de JavaScript!

Día 1

2

Variables, funciones y sintaxis básica

3

Tipos de datos

4

Playground - Retorna el tipo

5

Tipos de datos - pt 2

Día 2

6

Operadores

7

Hoisting y coerción

8

Playground - Calcula la propina

9

Alcance de las variables

Día 3

10

Condicionales

11

Playground - Calcula años bisiestos

12

Switch

13

Playground - Obten información de mascotas según su tipo

14

Ciclos

15

Playground - Dibuja un triangulo

Día 4

16

Arrays

17

Playground - Encuentra al michi mas famoso

18

Objetos

19

Playground - Obten el promedio de los estudiantes

Día 5 - Checkpoint

20

Playground - encuentra el palindromo más grande

Día 6

21

Reasignación y redeclaración

22

Modo estricto

Día 7

23

Debugging y manejo de errores

24

Programación funcional

Quiz: Día 7

Día 8

25

Closures

26

Playground - Crea una calculadora con closures

27

Higher order functions

28

Playground - Crea tu propio método map

Día 9

29

ECMAScript

30

TC39

Quiz: Día 9

Día 10 - Checkpoint

31

ES6

32

ES7

33

Playground - Task planner

Día 11

34

Asincronismo

35

Playground - Promesas

36

Manejando el asincronismo

37

Playground - Resuelve el callback hell usando promesas

38

Playground - Resuelve el callback hell usando async/await

Día 12

39

Arrays a profundidad

40

Métodos de arrays: Every, Find y findIndex

41

Playground - Válida el formulario

Día 13

42

Métodos de arrays: Includes, Join y concat

43

Playground - agrupa los productos

44

Métodos de arrays: Flat y FlatMap

45

Playground - Encuentra la ubicación del valor buscado

Día 14

46

Mutable functions

47

Playground - Modifica una lista de compras

48

Métodos de arrays: sort

49

Playground - Ordena los productos

Día 15 - Checkpoint

50

Playground - Sistema de reservaciones de un hotel

Día 16

51

Programación orientada a objetos en JavaScript

52

Objetos literales

53

Playground - Congela el objeto recursivamente

Día 17

54

Prototipos en JavaScript

55

Playground - Modifica el prototype de los arrays

56

Playground - Crea un auto usando clases

Día 18

57

Abstracción en JavaScript

58

Playground - Sistema de carrito de compras

59

Encapsulamiento en JavaScript

60

Playground - Encapsula datos de los usuarios

Día 19

61

Herencia en JavaScript

62

Playground - Jerarquía de animales

63

Polimorfismo en JavaScript

64

Playground - Sistema de pagos

Día 20 - Checkpoint

65

Playground - Agenda de vuelos

Día 21

66

Patrones de diseño

67

Sinlgeton y Factory pattern en JavaScript

68

Playground - Implementa singleton en un chat

Día 22

69

Adapter y Decorator pattern en JavaScript

70

Playground - Personaliza productos de una tienda

71

Builder y Protype pattern en JavaScript

72

Playground - Mejora el código usando builder pattern

Día 23

73

Facade y proxy pattern en JavaScript

74

Playground - Proxy en servicio de mensajería

75

Chain of responsability y Observer pattern en JavaScript

76

Playground - Implementación de Observador en Newsletter

Día 24 - Checkpoint

77

Playground - Crea un task manager con patrones de diseño

Día 25

78

Estructuras de datos en JavaScript

79

Playground - Crea tu propia implementación de un array

80

Hash tables en JavaScript

81

Playground - Implementación de una HashTable para Contactos

Día 26

82

Set en JavaScript

83

Playground - Remueve duplicados de una lista

84

Maps en JavaScript

85

Playground - Crea un organizador de tareas

Día 27

86

Singly Linked List en JavaScript

87

Playground - Agrega métodos a la singly linked list

88

Playground - Implementación de una singly linked list

Día 28

89

Stacks en JavaScript

90

Playground - Crea un stack para una playlist

Día 29

91

Queues en JavaScript

92

Playground - Crea una cola de emails

Día 30

93

¡Lo lograste!

Live Class

94

30 días de JS con Juan DC

95

30 días de JS con Nicobytes

96

30 días de JS con GNDX

97

30 días de JS con LeoCode

98

30 días de JS con Teffcode

99

Sesión: Cierre de los 30 días de JavaScript

No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Playground - Implementa singleton en un chat

68/99

Aportes 33

Preguntas 0

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

Mi solución.

import { User } from "./user";

export class Chat {
  constructor() {
    if (!Chat.instance) {
      this.users = [];
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }
  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
      return true;
    }
  }
  sendMessage(Message) {
      this.users.forEach(x => x.receiveMessage(Message));
      return true;
  }
  removeUser(name) {
      const deleted = this.users.findIndex(x => x.name === name);
      this.users.splice(deleted, 1);
      return true;
  }
}


.
.
.
.
.
.

import { User } from "./user";

export class Chat {
  constructor() {
    if (!Chat.instance) {
      this.users = [];
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }

  sendMessage(message) {
    this.users.forEach(user => user.receiveMessage(message));
  }

  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
    }
  }

  removeUser(name) {
    const deletedUser = this.users.findIndex(user => user.name === name);
    this.users.splice(deletedUser, 1);
  }
}

🛡️🛡️Escudo anti-spoilers🛡️🛡️

Mi solución al reto:

import { User } from "./user";

export class Chat {
  constructor() {
    //singleton
    if (!Chat.instance) {
      this.users = [];
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }

  sendMessage(message) {
    for (let user of this.users) {
      user.receiveMessage(message)
    }
  }

  addUser(user) {
    if (user instanceof User) {
      this.users.push(user)
    }
  }

  removeUser(name) {
    let indexToRemove = this.users.findIndex((u) => u.name == name)
    if (indexToRemove < 0) throw new Error('El usuario a eliminar no exister actualmente en la lista')
    return this.users.splice(indexToRemove, 1)
  }
}

my generic code

import { User } from "./user.js";

export class Chat {
  constructor() {
    this.users = []
    if (!Chat.instance) {
      Chat.instance = Object.freeze(this)
    }
    return Chat.instance
  }
  sendMessage(message) {
    for (const user of this.users) {
      user.messages.push(message)
    }
  }
  addUser(user) {
    if (user instanceof User) this.users.push(user)
  }
  removeUser(name) {
    const userIndex = this.users.findIndex((item) => item.name === name)
    this.users.splice(userIndex, 1)
  }
}

Mi solución:

Desafio Singleton completado:

Mi solución 💚 (uno de mis patrones favoritos es Singleton precisamente jeje)

import { User } from "./user";

export class Chat {
  constructor() {
    if (!Chat.instance) {
      this.users = []
      Chat.instance = Object.freeze(this)
    }
    return Chat.instance
  }

  sendMessage(message) {
    this.users.forEach(user => {
      user.receiveMessage(message)
    })
  }

  addUser(user) {
    if(user instanceof User)
      this.users.push(user)
  }

  removeUser(name) {
    let indexOfUser = this.users.findIndex(user => 
      user.name === name
    )
    
    if (indexOfUser !== -1) 
      this.users.splice(indexOfUser,1)
  }
}

🛡️🛡️🛡️Escudo anti spoilers🛡️🛡️🛡️

Implementa singleton en un chat

export class Chat {
  // Tu código aquí 👈
  constructor() {
    if (!Chat.instance) {
      this.users = [];
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }
  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
    }
  }
  removeUser(userName) {
    const userToDelete = this.users.findIndex((user) => user.name == userName);
    this.users.splice(userToDelete, 1);
  }
  sendMessage(mesagge) {
    this.users.forEach((user) => {
      user.messages.push(mesagge);
    });
  }
}
export class Chat {
  constructor() {
    this.users = []

    if (!Chat.instance) {
      this.name = "Chat";
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }

  addUser(user) {
    if (user instanceof User) {
      this.users.push(user)
    }
  }

  removeUser(name) {
    const index = this.users.findIndex(u => u.name === name)
    this.users.splice(index, 1)
  }

  sendMessage(message) {
    this.users.forEach(user => {
      user.receiveMessage(message)
    })
  }
}
export class Chat {
  users  = [];
  constructor() {
    if (!Chat.instance) {
      Chat.instance = this;
    }
    return Chat.instance;
  }
  sendMessage(message) {
    this.users.forEach((user) => {
      user.receiveMessage(message);
    });
  }
  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
    }
  }
  removeUser(name) {
    const result = this.users.findIndex((user) => user.name === name);
    this.users.splice(result, 1);
  }
}

Hola mi solución .
9️⃣
8️⃣
7️⃣
6️⃣
5️⃣
4️⃣
3️⃣
2️⃣
1️⃣
0️⃣

exercise.js

import { User } from "./user";
export class Chat {
  constructor() {
    if (!Chat.instance) {
      this.createInstance();
      Chat.instance = this;
    }
    return Chat.instance;
  }

  createInstance() {
    this.users = [];
  }

  sendMessage(message) {
    this.users.forEach((user) => {
      user.receiveMessage(message);
    });
  }

  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
    }
  }

  removeUser(user) {
    this.users = this.users.filter((u) => u.name !== user);
  }
}

. SPOILER ❤️

.
.
.

.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.

import { User } from "./user";

export class Chat {
  // Tu código aquí 👈
  users = [];

  //integrate singleton
  constructor() {
    if (!Chat.instance) {
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }

  sendMessage(message) {
    this.users.forEach((user) => {
      user.receiveMessage(message)
    })
  }

  addUser(user) {
    if (user instanceof User) this.users.push(user)
  }

  removeUser(name) {
    const indexUser = this.users.findIndex((user) => user.name === name)
    this.users.splice(indexUser, 1)
  }

}

.
.
.
.
.
.
.
.
.
.

import { User } from "./user";

export class Chat {
  constructor() {
    if (Chat.instance) {
      return Chat.instance;
    }

    this.users = [];
    Chat.instance = this;
  }

  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
    }
  }

  removeUser(name) {
    this.users = this.users.filter(user => user.name !== name);
  }

  sendMessage(message) {
    this.users.forEach(user => user.receiveMessage(message));
  }
}

Y seguimos avanzando lento pero seguimos, mi solucion:
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

exercise.js

import { User } from "./user";

export class Chat {
  // Tu código aquí 👈
  constructor() {
    if (!Chat.instance) {
      this.users = [];
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }
  sendMessage(message) {
    for (const user in this.users) {
      this.users[user].receiveMessage(message);
    }
  }
  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
      return true;
    }
  }
  removeUser(name) {
    const ind = this.users.findIndex(user => user.name === name);
    this.users.splice(ind, 1);
  }
}

MI SOLUCION 💪
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

import { User } from "./user";

export class Chat {
  users = [];

  constructor() {
    if (!Chat.instance) Chat.instance = Object.freeze(this);
    return Chat.instance;
  }

  sendMessage(message) {
    this.users.forEach(user => user.receiveMessage(message));
  }

  addUser(user) {
    if (user instanceof User) this.users.push(user)
  }

  removeUser(name) {
    const index = this.users.findIndex(user => user.name === name);
    this.users.splice(index, 1);
  }
}

Aquí mi solución:
.
.
.
.
.
.
.
.
.
.

export class Chat {
  constructor() {
    if (!Chat.instance) {
      this.users = []
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }

  sendMessage(message) {
    this.users.map(elem => elem.receiveMessage(message));
  }
  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
    }
  }
  removeUser(name) {
    let index = this.users.findIndex(elem => elem.name === name);
    if (index >= 0) {
      this.users.splice(index, 1);
    }
  }
}

Bueno dejo mi aporte, y retomo los retos porque los deje hace una semana ya que hacer POO me da pereza y me parece complicado pero si se empieza hay que terminar.

.
.
.
.
.
.
.
.
.
.
.
.

import { User } from "./user";

export class Chat {
  constructor() {
    if (!Chat.instance) {
      this.users = []
      Chat.instance = Object.freeze(this)
    }
    return Chat.instance
  }

  sendMessage(message) {
    this.users.forEach((user) => user.receiveMessage(message))
  }

  addUser(user) {
    if(user instanceof User) this.users.push(user)
  }

  removeUser(name) {
    const idx = this.users.findIndex((user) => user.name === name)
    this.users.splice(idx, 1)
  }
}

El nombre de la propiedad que tiene el array con todos los usuarios se debe llamar users para pasar la prueba, aquí dejo mi código… 😃

export class Chat {
  // Tu código aquí 👈
    constructor(){
        
        this.users =[];
        if (!Chat.instance){
            Chat.instance=this;        
          
        }else{
            return Chat.instance;
        }
    }
 sendMessage(message){
    this.users.forEach(user=>user.receiveMessage(message));
}

 addUser(user){
    if (user instanceof User){
        this.users.push(user);
        return this.list;
    }else{
        return this.users;
    }
}
removeUser(name){
    let index= this.users.findIndex(user=>user.name===name);
    this.users.splice(index,1);
    return this.users;
}
}

Mi solucion.;
.
.
.
.
.

import { User } from "./user";

export class Chat {
  constructor() { 
    if (!Chat.instance) { 
      this.users = [],    
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }
  sendMessage(message) {
    this.users.forEach(element => element.receiveMessage(message))
  };
  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
    }
  };
  removeUser(name) {
    let index = this.users.findIndex(user => user.name == name)
    this.users.splice(index, 1)
  };
}

Solución… 😄
.
Interesante reto, para el ejercicio en la clase Chat se debe intuir alguna propiedad a partir de la descripción de los métodos.
.
Para saber si un objeto es la instancia de una clase en específico se puede utilizar “instanceof”.
.
.
.
.

import { User } from "./user";

export class Chat {
  constructor() { 
    if (!Chat.instance) { 
      this.users = [];
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }

  sendMessage(message) {
    this.users.forEach((user) => { 
      user.receiveMessage(message);
    });
  }

  addUser(user) { 
    if (user instanceof User) { 
      this.users.push(user);
    }
  }

  removeUser(name) { 
    let index = this.users.findIndex(user =>
      user.name === name
    );
    this.users.splice(index,1); 
  }
}

Mi solución
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

import { User } from "./user";

export class Chat {
  users = []
  constructor() {
    !Chat.instance && (Chat.instance = Object.freeze(this))
    return Chat.instance
  }
  sendMessage(message) {
    this.users.map(user => user.receiveMessage(message))
  }
  addUser(user) {
    user instanceof User && this.users.push(user)
  }
  removeUser(name) {
    let index = this.users.findIndex(user => user.name === name)
    this.users.splice(index, 1)
  }
  showAddUsers() {
    return this.users
  }
}

Mi solución
.
.
.
.
.
.
.
.
.
.
.
.
.

import { User } from "./user";

export class Chat {
  // Tu código aquí 👈
  // Tu código aquí 👈
  constructor() {
    this.users = [];
    if (!Chat.instance) {
      this.name = 'Chat';
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }
  addUser(user) {
    if (user instanceof User) this.users.push(user);
  }
  sendMessage(message) {
    this.users.forEach(user => user.receiveMessage(message));
  }
  removeUser(name) {
    const index = this.users.findIndex((user) => user === name);
    this.users.splice(index, 1)
  }
}

Mi solución

export class Chat {
  users = [];
  constructor() {
    if (!Chat.instance) {
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }
  sendMessage(message) {
    for (let user of this.users) {
      user.receiveMessage(message);
    }
  }
  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
    }
  }
  removeUser(name) {
    const userIndex = this.users.findIndex(user => user.name == name);
    this.users.splice(userIndex, 1);
  }
}

freeze mmm

import { User } from "./user";

export class Chat {
  constructor() {
    if (!Chat.instance) {
      this.users = [];
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }
  sendMessage(message) {
    this.users.forEach(user => user.receiveMessage(message));
  }

  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
    }
  }

  removeUser(name) {
    const index = this.users.findIndex(user => user.name = name);
    if (index > -1) {
      this.users.splice(index, 1);
    }
  }
}

Mi solución:

export class Chat {
  constructor() {
    if (!Chat.instance) {
      this.users = [];
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }

  sendMessage(message) {
    for (let i=0; i<this.users.length; i++) {
      this.users[i].receiveMessage(message);
    }
  }
  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
    }
    return this;
  }
  removeUser(name) {
    for (let i=0; i<this.users.length; i++) {
        if (this.users[i].name===name){
            this.users.splice(i,1);
            return this;
        }
    }
    return this;
  }
}

Dejo mi solución:
.
.
.
.
.
.
.
.
.
.

import { User } from "./user";

export class Chat {
  // Tu código aquí 👈

  constructor() {
    this.users = [];

    if (!Chat.instance) {
      Chat.instance = Object.freeze(this);
    }
    
    return Chat.instance;
  }

  sendMessage(message) {
    for (const user of this.users) user.receiveMessage(message);
  }

  addUser(user) {
    if (user instanceof User) this.users.push(user);
  }

  removeUser(name) {
    const index = this.users.findIndex(user => user.name === name);
    if (index == -1) throw new Error("El usuario no existe");
    this.users.splice(index, 1);
  }
}

Les dejo mi solución:
.
.
.
.
.
.

import { User } from "./user";

export class Chat {
  constructor() {
    if (!Chat.instance) {
      this.users = [];
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }
  sendMessage(message) {
    for (let user of this.users) {
      user.receiveMessage(message)
    }
  }
  addUser(user) {
    if (user instanceof User) this.users.push(user);
  }
  removeUser(name) {
    this.users.splice(this.users.indexOf(this.users.find(user=>user.name===name)),1)
  }
}

Mi solución:
.
.
.
.
.
.
.

import { User } from "./user";

export class Chat {
  // Tu código aquí 👈
  constructor() {
    if (!Chat.instance) {
      this.users = []
      Chat.instance = Object.freeze(this)
    }
    return Chat.instance
  }
  sendMessage(message) {
    for (let user of this.users) {
      user.receiveMessage(message)
    }
  }
  addUser(user) {
    if (user instanceof User) {
      this.users.push(user)
    }
  }
  removeUser(name) {
    const index = this.users.findIndex((user) => user.name === name)
    if (index !== -1) {this.users.splice(index, 1)}
  }
}

✅Mi solución:
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

import { User } from "./user";

export class Chat {
  constructor() {
    if (!Chat.instance) {
      this.users = [];
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }

  sendMessage(message) {
    this.users.forEach(user => {
      user.receiveMessage(message);
    });
  }

  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);
    }
  }

  removeUser(name) {
    const isUserInList = this.users.map(user => user.name)
      .includes(name);
    if (isUserInList) {
      const index = this.users.findIndex(user => user.name === name);
      this.users.splice(index, 1);
    }
  }
}

**Mi respuesta **

export class Chat {
  constructor(name) {
    if (!Chat.instance) {
      this.users = [];
      Chat.instance = Object.freeze(this);
    }
    return Chat.instance;
  }

  addUser(user) {
    if (user instanceof User) {
      this.users.push(user);

    }
  }

  sendMessage(message) {
    this.users.forEach(user => {
      user.receiveMessage(message);
    });
  }

  removeUser(name) {
    for (var i = 0; i < this.users.length; i++) {
      if (this.users[i].name === name) {
        this.users.splice(i, 1);
      }
    }
  }
}

Solución:

import { User } from "./user";

let users = new Array(User)
export class Chat {
  // Tu código aquí 👈
  constructor() {
    this.users = []
    if (!Chat.instance) {
      Chat.instance = Object.freeze(this)
    }
    return Chat.instance
  }
  sendMessage(message) {
    this.users.forEach(element => {
      element.receiveMessage(message)
    })
  }
  addUser(user) {
    if (user instanceof User) {
      this.users.push(user)
    }
  }
  removeUser(name) {
    this.users = this.users.filter(element => element.name !== name)
  }
}

Mi solución
*
*
*
*
*
*

import { User } from "./user";

export class Chat {

  constructor() {
    this.users = []
    if (!Chat.instance) {
      Chat.instance = this
    }
    return Chat.instance;
  }

  sendMessage(message) {
    this.users.forEach(user => {
      user.receiveMessage(message)
    })
  }

  addUser(user) {
    if (user instanceof User) {
      this.users.push(user)
    } 

  }

  removeUser(name) {
    this.users = this.users.filter(user => user.name !== name)
  }
}
import { User } from "./user";

export class Chat {
   
  constructor() {
    if (!Chat.instance) {
      this.users = []
      Chat.instance = Object.freeze(this);
    }
    
    return Chat.instance;
  }
  sendMessage(message) {
    this.users.forEach(user => user.receiveMessage(message))
  }

  addUser(user) {
    if (user instanceof User) {
      this.users.push(user)
    }
  }

  removeUser(nameUser) {
    const index = this.users
      .findIndex(({ name }) => name === nameUser)

    if (index >= -1) {
      this.users.shift(index, 1)
    }
  }
}

undefined