Published
Edited
Aug 2, 2022
1 star
Insert cell
Insert cell
/*
* Pick random from given array, return Object of random item and its index
* @param {Array} arr Array of items
* @return {Object} Object of random value and its index in original array
*/
function pick(arr) {
let index = Math.floor(Math.random() * arr.length)
return({
value: arr[index],
index
})
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
testArr = ['apple', 'orange', 'banana', 'kiwi', 'grape']
Insert cell
myPick = {
button1
return pick(testArr)
}
Insert cell
myPick.value
Insert cell
myPick.index
Insert cell
Insert cell
Insert cell
Insert cell
letter = {
button2
let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
return pick(letters)
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more