“Cara Menghasilkan Array Nomor Acak Di JavaScript” Kode Jawaban

JavaScript mendapatkan nilai array acak

//get random value from array
var colors = ["red","blue","green","yellow"];
var randColor = colors[Math.floor(Math.random() * colors.length)];
Grepper

JavaScript mendapatkan nilai array acak

const months = ["January", "February", "March", "April", "May", "June"];

const random = Math.floor(Math.random() * months.length);
console.log(random, months[random]);
OHIOLee

JavaScript Cara Mendapatkan Elemen Acak dari Array

var items = ['Yes', 'No', 'Maybe'];
var item = items[Math.floor(Math.random() * items.length)];
Helpless Hornet

cara mendapatkan elemen acak dari javascript array

var foodItems = ["Bannana", "Apple", "Orange"];
var theFood = foodItems[Math.floor(Math.random() * foodItems.length)];
/* Will pick a random number from the length of the array and will go to the
corosponding number in the array E.G: 0 = Bannana */
Your moms hornyness

JavaScript mendapatkan item acak dari array

const list = [1, 2, 3, 4, 5, 6];

// shuffle your list with the sort function:
const shuffledList = list.sort(() => Math.random() - 0.5);
// generate a size for the new list
const newListSize = Math.floor(Math.random() * list.length)
// pick the first "newListSize" elements from "shuffledList"
const newList = shuffledList.slice(0, newListSize)

console.log(newList);
// [3, 2, 6]; [5]; [4, 1, 2, 6, 3, 5]; []; etc..
Dark Dotterel

Cara Menghasilkan Array Nomor Acak Di JavaScript

// how to generate an array of random numbers in javascript
const randomIntArrayInRange = (min, max, n = 1) =>
  Array.from(
    { length: n },
    () => Math.floor(Math.random() * (max - min + 1)) + min
  );
console.log(randomIntArrayInRange(5, 20, 6)); // [ 11, 7, 17, 13, 12, 16 ]
Chetan Nada

Jawaban yang mirip dengan “Cara Menghasilkan Array Nomor Acak Di JavaScript”

Pertanyaan yang mirip dengan “Cara Menghasilkan Array Nomor Acak Di JavaScript”

Lebih banyak jawaban terkait untuk “Cara Menghasilkan Array Nomor Acak Di JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya