Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
Randomly push names into one of two arrays

I have an object with two arrays (_licky, _unlucky). and methods that randomly push names into one of the two arrays. But my code does not work for some reason... What is wrong with my code?

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);

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I fixed it. Now it works. thank you all!!

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);
about 4 years ago · Juan Pablo Isaza Relatório

0

There were a few small errors on your part. I have rewritten it in a class variant. It works, but I would still improve a few things. You can test it a bit.

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());

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda