+ 1
Random
https://code.sololearn.com/WDQuK26t1hdi/?ref=app Check this.
4 Answers
+ 4
+ 2
What you do is storing a random value in a variable and printing it twice, which will produce the same output.
What you can do is calling the Math.random twice in appropriate places
+ 2
Output is same because you are calling random function once and assigning in a single variable.
To get different value call random function twice
if you want different values then do like this
let arr = [1,2,3,4,5];
console.log(Math.floor(Math.random() * arr.length));//first number
console.log(Math.floor(Math.random() * arr.length));//second number
OR
let arr = [1,2,3,4,5];
for(var i = 0; i < 2; i++) {
let random = Math.floor(Math.random() * arr.length);
console.log(random);//first number
}
OR
as Sivaprasad Did
+ 1
https://code.sololearn.com/W5cHEZZfqBpJ/?ref=app
Output is same because you are storing random number is a single variable.
different values please link this