Tengo un objeto con dos matrices (_licky, _unlucky). y métodos que insertan nombres aleatoriamente en una de las dos matrices. Pero mi código no funciona por algún motivo... ¿Qué problema tiene mi código?
const luckGame = { _lucky: [], _unlucky: [], pushGamer(name) { return name; }, getRandomNumber (random) { return random = Math.floor(Math.random() * 2); }, pushGamerIntoArray () { return { if (this.getRandomNumber() === 0 ) { this._lucky.push(this.pushGamer()); } else { this._unlucky.push(this.pushGamer()); } }; }, }; this.pushGamer('John'); this.pushGamer('Nick'); this.pushGamer('Maria'); this.pushGamer('Sarah'); this.pushGamer('Ron'); this.pushGamer('Lisa'); console.log(luckGame._lucky); console.log(luckGame._unlucky);Lo arreglé. Ahora funciona. ¡¡gracias a todos!!
const luckGame = { _lucky: [], _unlucky: [], getRandomNumber (random) { return random = Math.floor(Math.random() * 2); }, pushGamerIntoArray (name) { if (this.getRandomNumber() === 0 ) { return this._lucky.push(name); } else { return this._unlucky.push(name); } }, }; luckGame.pushGamerIntoArray('John'); console.log(luckGame._lucky); console.log(luckGame._unlucky);Hubo algunos pequeños errores de su parte. Lo he reescrito en una variante de clase. Funciona, pero aún mejoraría algunas cosas. Puedes probarlo un poco.
const luckyGame = class { constructor() { this._lucky = [], this._unlucky = [] } pushGamer(name) { this.pushGamerIntoArray(name) } getRandomNumber (random) { return random = Math.floor(Math.random() * 2); } pushGamerIntoArray (newGamerName) { if (this.getRandomNumber() === 0 ) { this._lucky.push(newGamerName) } else { this._unlucky.push(newGamerName) } } getLuckies () { return this._lucky } getUnluckies () { return this._unlucky } } let luckygame = new luckyGame() luckygame.pushGamer('John'); luckygame.pushGamer('Nick'); luckygame.pushGamer('Maria'); luckygame.pushGamer('Sarah'); luckygame.pushGamer('Ron'); luckygame.pushGamer('Lisa'); luckygame.pushGamer('John'); console.log(luckygame.getLuckies()); console.log(luckygame.getUnluckies());