N
N
nikvay2020-06-02 17:46:56
Angular
nikvay, 2020-06-02 17:46:56

How to implement randomize a random number and display a block under this number?

Hello!
I need to implement the following: the system randomizes a random number in a given range (let it be from 1 to 5), the user presses the button, the system displays a block for him under the previously randomized number. The text block is rendered using HTLM.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
noob noob, 2020-06-02
@nikvay

If I understand you correctly, then it is very simple to do this in
HTML:

<a onclick="randomDiv()">click</a>

<div id="random1" style="display: none;">Random div 1</div>
<div id="random2" style="display: none;">Random div 2</div>
<div id="random3" style="display: none;">Random div 3</div>
<div id="random4" style="display: none;">Random div 4</div>
<div id="random5" style="display: none;">Random div 5</div>

JS:
function randomDiv(){
  let randNum = getRandomInRange(1, 5);
  document.getElementById('random'+randNum).style.display = "block";
}

Function to get a random number in a range
function getRandomInRange(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question