How to generate random number between 1 and 10 in JavaScript

2 Answers

0 votes
const n = Math.floor((Math.random() * 10) + 1); 
		
console.log(n);




/*
run:
 
6
  
*/

 



answered Jun 13, 2015 by avibootz
edited Jun 6, 2022 by avibootz
0 votes
for (let i = 0; i < 5; i++) {
	let n = Math.floor((Math.random() * 10) + 1); 
	console.log(n);
}




/*
run:
 
10
4
6
1
4
  
*/

 



answered Jun 13, 2015 by avibootz
edited Jun 6, 2022 by avibootz

Related questions

1 answer 245 views
1 answer 227 views
1 answer 201 views
2 answers 290 views
1 answer 367 views
1 answer 287 views
...