+ 1
I want to create a random number generator using JavaScript?
The array must contains 23 x '1', 4 X number '2', 7 x' 5' 4 x '10', two strings 'chance' three strings 'tworolls' and one string called '4 rolls'
5 Respostas
+ 3
<!DOCTYPE html>
<html>
<head>
<title>random number in javascript</title>
</head>
<body>
<script>
//get random number
var rndnmb=Math.floor(Math.random()*100);
document.write("<b>the random number is </b>"+rndnmb);
</script>
</body>
</html>
Try it
+ 2
Tharun Here is the solution :
Code :
const array = [ /*your array */ ];
const randomIndex = Math.floor(Math.random() * array.length);
const randomElem = array[randomIndex];
console.log(randomElem);
Here is a demo :
https://code.sololearn.com/W54yd8ILd5AT/?ref=app
+ 1
Hey Tharun, I didn’t understand your description. Please explain a bit more :))
+ 1
Array =[
1,
1,
1,......(23 times)
2,
2,.....(14 times),
5,
5,...(7 times),
10,
10,
10,
10,
'2rolls',
'2rolls',
'2rolls',
'chance',
'chance',
'4roll',
]
I want to generate random elements from this array using JavaScript
0
May I know how this is not working. I want random element to go on a loop continuously
let Array =[
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"1",
"2",
"2",
"2",
"2",
"2",
"2",
"2",
"2",
"2",
"2",
"2",
"2",
"2",
"2",
"5",
"5",
"5",
"5",
"5",
"5",
"5",
"10",
"10",
"10",
"10",
"2roll",
"2roll",
"2roll",
"chance",
"chance",
"4roll",
]
var t = 0;
var i = 10;
const randomIndex = Math.floor(Math.random() * Array.length);
const randomElem = Array[randomIndex];
console.log(randomElem);
for (var element of randomElem )
{
if(element ===1)
{
t = t + i;
console.log(element )
console.log(t)
console.log(i)
}
else {
t = t - i;
i = i * 2;
console.log(element )
console.log(t)
console.log(i)
}
}