Array, range selection, pickRandom

I’d like to create an array of say 100 items. For example var p[100];
and select a range of consecutive items:
say p[28] thru p[37], and pick an item randomly out of the selected range.
Can I code like:
var gg = pickRandom(p[28]:p[38]); ?
Thank you.

Let’s say you already have an array of 100 items, myArray. You can select the elements from index 28 to 37 (including those elements) using the .slice() method.

myArray.slice(27,37) would give you [myArray[28], myArray[29], ..., myArray[37]].

You can put that inside of pickRandom() to get a random element from that slice of the array: pickRandom(myArray.slice(27,37)).



Thank you very much for your help!

Your input is exactly what I was looking for.

Is there any place on-line I should/can go to when I need info on functions/methods other than .slice()?

Thank you again.


MDN has very good documentation.

Here’s the page for .slice(). The column on the left side lists all of the other array methods. If you go up a level by removing “/slice” from the URL, you can see examples for a bunch of array methods.


You’ve been so helpful! I’m really greatful for your help.

1 Like