JavaScript selecting random numbers(for color generation) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JavaScript selecting random numbers(for color generation)

var letters = '0123456789ABCDEF'; letters[Math.floor(Math.random() * 16)] Here why have we used [ ] and also can we take any number instead of 16 ?

14th Sep 2019, 7:04 PM
harshit
harshit - avatar
1 Answer
+ 2
the [] are to get a certain character from the String 'letters' the 16 is just the length of the String letters, you could use smaller values too, but using negative values or values greater than 16 can result in errors, since the index wouldn't be present in the String anymore ... hope this helped happy coding ! 😄
14th Sep 2019, 7:14 PM
Anton Böhler
Anton Böhler - avatar