Random Number Generator
1
Create react app
2
In App.js,
Create a div inside which there are sections and forms.
Create useState diceCount, diceType, result
where diceCount is no of dices
diceType is no of sides in a dice,
result is the end result.
Create an input whose value is{diceCount} and onChange setdiceCount is set to the target value.
Create an select whose value is{diceType} and onChange setdiceType is set to the target value.
Options are given with respective value.
A constant dice is created with prop side which generates random number.
A constant count and type take the value of diceCount and diceType resp.
A constant handleSubmit is created with an arrow function of prop e,
e.preventDefault() is done to prevent the page to refresh when submitted and prevent default value.
if count and type both are present then,
constant spin is created which is an empty array.
A loop is created which takes,
i=0;i<parseInt(count);i++;
which means the loop goes on until i is less than the count .
where, a constant Roll is dice(parseInt(type)) and Roll is pushed to spin.
setResult is set to spin.
A section is created where result.map is done to display all the values of an array present in spin.
UseEffect is created which consoles so that error doesnt occur.
Some styling is done.
Comments
Post a Comment