Home > other >  Adding random number as textContent to each of my 25 divs?
Adding random number as textContent to each of my 25 divs?

Time:11-26

basically im working on a "simple game"....which is not so simple. I created 25 divs which are squares in my game. I would like to assign random number to each square from 1-25 every time a user hits start game. I understand i need to loop through it and then assign a random number to it.

  1. I created a random number via Map.Floor and Map.Random.
  2. I have my divs saved into variable called squares inside an array.
  3. I tried running loops, map, forEach....but when i add textContent to it, i get the same random number in ALL of my squares, instead of assigning a random number to each square?

To my understanding, after i assign [i] to my squares, i am running through them, why is it not assigning a random number each time it loops?

Thank you very much for your help and explanation!!

let btn = document.querySelector('.btn')
const squares = [...document.querySelectorAll('.square')];

const random = Math.floor(Math.random() * 25)

btn.addEventListener('click', startGame)




function startGame() {
let randomNumber


    for (let i = 0; i < squares.length; i  ) {
        randomNumber = squares[i].textContent = random;
        
    }
}
* {
    margin: 0;
    padding: 0;
    }
    
    body, html {
    min-width: 100%;
    min-height: 100vh;
    box-sizing: border-box;
    font-size: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: black;
    }
    
    
    img {
        max-width: 100%;
        display: block;
    }

   
    main {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 24%;
        background-color: white;
        border-radius: 10px;
    }
<body>
    <main>
      <div >
        <p >Time left:</p>
      </div>
      <grid >
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
        <div ></div>
      </grid>
      <button >Start Game</button>
    </main>
  </body>

CodePudding user response:

It's because you have created only one random number. If you declare it inside for loop, on each iteration it's going to be different

let btn = document.querySelector('.btn')
const squares = [...document.querySelectorAll('.square')];

btn.addEventListener('click', startGame)

function startGame() {
let randomNumber

    for (let i = 0; i < squares.length; i  ) {
        const random = Math.floor(Math.random() * 25)
        randomNumber = squares[i].textContent = random;
    }
}

I am not sure what's the purpose of the randomNumber variable though. You can do:

function startGame() {
    for (let i = 0; i < squares.length; i  ) {
        const random = Math.floor(Math.random() * 25)
        squares[i].textContent = random;
    }
}

and it will work just fine

  • Related