Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

121
Vistas
Click event running once

I have two dice. When I click on the button, the dice are randomized. But, when I click again on the button, the function no longer runs. What can I do to run the function each time I click the button ?

let arr = ["dice1.png", "dice2.png","dice3.png","dice4.png","dice5.png","dice6.png" ];

let dice1 = document.querySelector(".dice1"); 
let dice2 = document.querySelector(".dice2"); 

let rand = arr[Math.floor(Math.random() * arr.length)]
let rand2 = arr[Math.floor(Math.random() * arr.length)]

let button = document.querySelector("button"); 


function game () {

let title = document.querySelector("h1"); 
dice1.src = "/images/" + rand
dice2.src = "/images/" + rand2

if (rand > rand2) {
    title.textContent = "Player 1 WIN"
}
else if (rand2 > rand) {
    title.textContent = "Player 2 WIN"
}
else if (rand2 === rand) {
    title.textContent = "EQUALS"
}
else{
    return
}

}


button.addEventListener ("click", () => {
    game(); 
})
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you move your rand and rand2 variable definitions inside of game() it should work. You're currently only generating the random #s when the script initially loads (not on button click), and then they're being used on button click each time, but the #s aren't being regenerated so it appears like it's not working.

Additionally, you can call game() on page load if you want it to run the first time without a button click.

Note: I made up some example HTML because it wasn't included in your question, but this should give you an idea of how to do it.

let arr = ["dice1.png", "dice2.png","dice3.png","dice4.png","dice5.png","dice6.png" ];

let dice1 = document.querySelector(".dice1"); 
let dice2 = document.querySelector(".dice2"); 

let button = document.querySelector("button");
let title = document.querySelector("h1"); 

function game () {
    // regenerate these each time this function is run
    let rand = arr[Math.floor(Math.random() * arr.length)]
    let rand2 = arr[Math.floor(Math.random() * arr.length)]

    dice1.src = "/images/" + rand
    dice2.src = "/images/" + rand2

    if (rand > rand2) {
        title.textContent = "Player 1 WIN"
    }
    else if (rand2 > rand) {
        title.textContent = "Player 2 WIN"
    }
    else if (rand2 === rand) {
        title.textContent = "EQUALS"
    }
    else{
        return
    }
}


button.addEventListener ("click", () => {
    game(); 
})

game(); // run it on page load if desired
<h1>title</h1>

<img class="dice1" />
<img class="dice2" />

<button>Roll</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Because you initial this block only once.

let arr = ["dice1.png", "dice2.png","dice3.png","dice4.png","dice5.png","dice6.png" ];

let dice1 = document.querySelector(".dice1"); 
let dice2 = document.querySelector(".dice2"); 

let rand = arr[Math.floor(Math.random() * arr.length)]
let rand2 = arr[Math.floor(Math.random() * arr.length)]

let button = document.querySelector("button"); 

But you need the values rand, rand2 on every click. Put this two vars into your code function.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda