How to randomly choose an item from a list in Javascript

3767 views javascript
3

I'm creating a game in buildbox, and i'm trying to create a script that is written in javascript(I am not a javascript coder) to choose randomly from a range of colors, for example:

color_list = [color1,color2,color3]

So that it in this list it picks color2 randomly taking into account color 2 is: (255,0,0) in RGB

Thanks for the help!

answered question

2 Answers

9

You could do:

let randomColor = color_list[Math.floor(Math.random() * color_list.length)];

As mentioned in Getting a random value from a JavaScript array

posted this
8

In your procedure, you explicitly specified more columns than the values you passed.

posted this

Have an answer?

JD

Please login first before posting an answer.