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

138
Visualizações
Am I just misusing objects in this random lotto generator code?

I've gotten a Random Lotto Generator script to work in JavaScript in one game. Now, I'm trying to get it to function for multiple Lotto games and figured Objects were the best way to adjust to the different formats of each game to account for different number pools, different selected numbers, 'bonus balls', duplicate numbers, etc.

const game = [{name: "Lotto Texas", balls: 6, numbers: 54, start: 1, duplicates: false, bonusBall: false, results: []},
{name: "Pick 3", balls: 3, numbers: 10, start: 0, duplicates: true, bonusBall: false, results: []},
{name: "Powerball", balls: 5, numbers: 69, start: 1, duplicates: false, bonusBall: true, bonusNumbers: 26, results: [], bonus: []}];

game.forEach (lottoResults()); // gets results

function lottoResults(){
    this.results.length = this.balls; //Here is where the error occurs. This is supposed to assign the balls property to the results array's length value.
 
    if (!this.duplicates){ //checks if duplicates allowed
        this.results.forEach(duplicateCheck());
    }
    else{
       this.results.forEach(lottoBall())
    }
    
    if(this.bonusBall){
        this.bonus.push(lottoBall(this.bonusNumbers, this.start)); //assigns a number to bonus ball
    }
}

I'm trying to set a blank array initialized in each object, with the amount with the 'balls' property (referring to lotto balls) setting how big the array is dependent on the game. However, anytime I try to set that up, I keep getting an Uncaught TypeError: Undefined is not a function. I've tried setting the object property value to a primitive variable, establishing the array length within the initialized object variables, but still get the same error in some form. Am I just misusing the object and should be calling the values differently?

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

0

You are invoking lottoResults here within forEach by placing () after the function name

game.forEach (lottoResults());

Instead, you should pass the function like this

game.forEach (lottoResults);

ref: https://www.w3schools.com/jsref/jsref_foreach.asp

Note: Same thing applies to duplicateCheck and lottoBall passed to forEach, remove the ()

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