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

279
Vistas
How to randomly select divs from inside another div?

I am struggling to randomly select some divs (9 in this case) to place bombs/mines for those selected tiles.

My code for this is:

    function place_mines() {

    if (document.getElementById("difficulty").selectedIndex == "2") {

        for (var u = 0; u < 8; u++) {

        }

    }
}

enter image description here Inspected the element, grid which contains all the tiles.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I would use a different approach. I would make a list of tile objects and generate the html from the list. This lets you flag an object as bomb without revealing it to the user by adding it as class name.

After you generated the list, you can take a random subset and flag it as bombs. You could use this approach for example: Sampling a random subset from an array.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I suggest you to give id to the tiles, to later you generate random id , and map the generated id to the tiles

about 4 years ago · Juan Pablo Isaza Denunciar

0

What worked for me was I added a counter to index the elements. After that, made an array of 9 random numbers from 0-80 (counter starts at 0), checked if the id of the tile selected is in that array, if it was then that tile has a mine else its a normal tile.

tile.setAttribute("id",counter);counter++;
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