Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!