JSFiddle - React, Tailwind, and code Playground
by Alexis López Espinoza
JavaScript
var juego = {
jugadores: null,
turno: null,
ganador: {nombre: null, valor: 0},
mensaje: "",
registrar: function(){
this.jugadores = [].slice.call(arguments);
return this;
},
lanzar: function(){
var aux, ganadores = [], body = document.body, br = "<br />";
this.jugadores.forEach(function(jugador){
aux = this.numero();
body.innerHTML += jugador + " obtuvo " + aux + br;
if (aux > this.ganador.valor){
this.ganador = {
nombre: jugador,
valor: aux
};
this.mensaje = "El ganador es " + jugador + " con " + aux + " puntos";
}
else if (aux == this.ganador.valor){
if (!ganadores.length) ganadores.push(this.ganador.nombre, jugador);
else ganadores.push(jugador);
this.mensaje = "Hay un empate entre: " + ganadores.join(", ") + " con " + aux + " puntos";
}
}, this);
body.innerHTML += br + this.mensaje;
},
numero: function(){
return Math.floor(Math.random() * 6) + 1;
}
};
juego.registrar("Juan", "Mario", "Estela", "Felicia").lanzar();