<div id="app"></div>
.main{
display: flex;
flex-direction:column;
align-items: center;
justify-content: center;
/* width:100wh; */
/* height:100vh; */
}
.dice-input{
width:8rem;
}
.rollbutton{
width:8rem;
margin-top: 1rem;
}
.dice{
padding:4px;
border: 1px solid black;
width: 50px;
height: 50px;
}
.dice-1{
display:flex;
align-items: center;
justify-content: center;
}
.dice-1{
display:flex;
align-items: center;
justify-content: center;
}
.dice-2{
display:flex;
justify-content: space-between;
}
.dice-2 .eye:nth-of-type(2){
align-self:flex-end;
}
.dice-3{
display:flex;
justify-content: space-between;
}
.dice-3 .eye:nth-of-type(2){
align-self:center;
}
.dice-3 .eye:nth-of-type(3){
align-self:flex-end;
}
.dice-4{
display:flex;
justify-content:space-between;
}
.dice-4 div{
display:flex;
flex-direction: column;
justify-content: space-between;
}
.dice-5{
display:flex;
justify-content:space-between;
}
.dice-5 div{
display:flex;
flex-direction: column;
justify-content: space-between;
}
.dice-5 div:nth-of-type(2){
align-self:center;
}
.dice-6{
display:flex;
justify-content: space-between;
}
.dice-6 div{
display:flex;
flex-direction: column;
justify-content: space-between;
}
.eye{
width: 10px;
height:10px;
background-color: black;
border-radius:50%;
}
.dice-container{
margin-top:20px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
column-gap: 10px;
row-gap: 15px;
}
/*
* https://frontendeval.com/questions/rolling-dice
*
* Create a dice roller application that can roll anywhere from 1-99 six-sided dice
*/
const rollNumberOfDice = (numberOfDiceToRoll) => {
var results = []
for(let i=0; i < numberOfDiceToRoll; i++){
results.push(Math.floor(Math.random() * 6 + 1))
}
return results;
}
const Dice = ({eyes}) => {
if(eyes <= 3){
return <div class={`dice dice-${eyes}`}>
{[... Array(eyes).keys()].map(el => <div class="eye"/>)}
</div>
}
if(eyes === 4){
return <div class="dice dice-4">
<div>
<div class="eye"/>
<div class="eye"/>
</div>
<div>
<div class="eye"/>
<div class="eye"/>
</div>
</div>
}
else if(eyes === 5){
return <div class="dice dice-5">
<div>
<div class="eye"/>
<div class="eye"/>
</div>
<div>
<div class="eye"/>
</div>
<div>
<div class="eye"/>
<div class="eye"/>
</div>
</div>
}
else if(eyes === 6){
return <div class="dice dice-6">
<div>
<div class="eye"/>
<div class="eye"/>
<div class="eye"/>
</div>
<div>
<div class="eye"/>
<div class="eye"/>
<div class="eye"/>
</div>
</div>
}
}
const App = () => {
const [formValue, setFormValue] = React.useState();
const [diceValues, setDiceValues] = React.useState();
const [rolled, setRolled] = React.useState(false);
const handleClick = () => {
if(!formValue){
alert("You need to set number of dice, otherwise we cannot roll.")
}
else if(formValue < 1 || formValue > 100){
alert("You need to throw between 1 to 99 dice")
}
else{
setDiceValues(rollNumberOfDice(formValue))
}
}
return <div class="main">
<h3>Number of dice</h3>
<input class="dice-input" placeholder="e.g. 63" type="number" value={formValue} onChange={e=>setFormValue(e.target.value)}/>
<button onClick={handleClick} class="rollbutton">Roll</button>
<div class="dice-container">
{diceValues && diceValues.map((val, idx) => {
return <Dice eyes={val}/>
})}
</div>
</div>
}
ReactDOM.render(<App />, document.getElementById('app'));
View Compiled
This Pen doesn't use any external CSS resources.