I want my button to generate 3 different numbers between 1 and 3 every time it's clicked and the generated numbers displayed in different html elements.
I am creating a button for my "guessGame". I have already created the interface which includes the buttons and div elements to store the output of t generated numbers. However, I want different numbers between 1 and 3 generated without collisions and stored in each div element. I am using javaScript
HTML
<div class="scores">
<div class="scoreDisplay scoreDisplay--1"></div>
<div class="scoreDisplay scoreDisplay--2"></div>
<div class="scoreDisplay scoreDisplay--3"></div>
</div>
<a href="#" id="startGame">Start Game</a>
css
.scores{
position:absolute;
left:50%;
top: 25%;
transform:translate(-50%,-50%);
}
.scoreDisplay{
width:100px;
height:100px;
border:1px solid black;
display:inline-block;
margin:10px;
font-size:3rem;
text-align:center;
padding-top:20px;
&--1{
background:yellow;
}
&--2{
background:orangered;
}
&--3{
background:green;
}
}
#startGame{
text-decoration:none;
text-transform:uppercase;
padding: 15px 45px;
border: 1px solid blue;
border-radius:5px;
color:blue;
position:absolute;
left:50%;
top:60%;
transform:translate(-50%,-50%);
margin-top:20px;
&:hover{
background:blue;
color:white;
}
}
JavaScript
var scoreDisplay = document.querySelectorAll(".scoreDisplay");
var startGame = document.querySelector("#startGame");
startGame.addEventListener("click", function(){
for(var i=0; i<scoreDisplay.length; i++){
scoreDisplay[i].textContent = Math.floor(Math.random() * 3) + 1;
}
})
I want 3 different numbers generated and displayed in each div element without collision. What I have now the numbers generated at times are the same at times in 2 or all div elements where they are displayed.
Here's the link https://codepen.io/myportfolios/pen/BvWYdK in codepen.
Thanks all for your anticipated responses. Happy Coding!