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

234
Views
How to get same random number using Math.random at different places?

randomNum function returns the different values to diceNum.innerText and player.style.gridColumnStart, I want them to be same,How do I do that ?

const randomNum = () => {
  return Math.floor(Math.random() * 6) + 1;
};

diceBtn.addEventListener("click", () => {
  const diceNum = document.getElementById("dice-num");
  diceNum.innerText = randomNum();
  drawPlayer();
});

function drawPlayer() {
  playerBody.forEach((pos) => {
    const player = document.createElement("div");
    player.style.gridRowStart = pos.x;
    player.style.gridColumnStart = randomNum();
    player.setAttribute("id", "playerElement");
    gameBoard.appendChild(player);
  });
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can pass the random generated number to the drawPlayer function, here's a sample

const randomNum = () => {
    return Math.floor(Math.random() * 6) + 1;
};

diceBtn.addEventListener( "click", () => {
  const diceNum = document.getElementById( "dice-num" );
  const rndNum = randomNum();
  diceNum.innerText = rndNum;
  drawPlayer( rndNum );
});

function drawPlayer( rndNum ) {
  document.querySelectorAll( ".playerBody" ).forEach( element => {
    element.innerText = rndNum;
  });
}
<button id="diceBtn">Roll</button>
<div id="dice-num">#</div>

<div class="playerBody">0</div>
<div class="playerBody">1</div>
<div class="playerBody">2</div>
<div class="playerBody">3</div>
<div class="playerBody">4</div>
<div class="playerBody">5</div>
<div class="playerBody">6</div>

about 4 years ago · Juan Pablo Isaza Report

0

Set randomNumber as a variable and use that.

class RanNumber {
    value = Math.floor(Math.random() * 6) + 1;
    
    generateNewNumber() {
    this.value = Math.floor(Math.random() * 6) + 1;
    }
    
    getNumber() {
      return this.value
    }


  }

const RanNum = new RanNumber()
   

diceBtn.addEventListener("click", () => {
  // On click generate new number
  RanNum.generateNewNumber()
  const diceNum = document.getElementById("dice-num");
  diceNum.innerText = RanNum.getNumber();
  drawPlayer();
});

function drawPlayer() {
  playerBody.forEach((pos) => {
    const player = document.createElement("div");
    player.style.gridRowStart = pos.x;
    player.style.gridColumnStart = RanNum.getNumber();
    player.setAttribute("id", "playerElement");
    gameBoard.appendChild(player);
  });
}
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!