“JavaScript Acak Sortir Random” Kode Jawaban

Bagaimana cara menyortir javascript array secara acak

array.sort(() => Math.random() - 0.5);
Real Rocket Raccoon

JS Shuffle Array

yourArray.sort(function() { return 0.5 - Math.random() });
Slocaly

JavaScript secara acak mengocok array

function randomArrayShuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;
  while (0 !== currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
  return array;
}
var alphabet=["a","b","c","d","e"];
randomArrayShuffle(alphabet); 
//alphabet is now shuffled randomly = ["d", "c", "b", "e", "a"]


Grepper

JavaScript Acak Sortir Random

// O(n)
function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
}
garzj

cara mengacak javascript array

/**
 * Shuffles array in place.
 * @param {Array} a items An array containing the items.
 */
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = round(random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}

shuffle(array);
Timmber

Shuffle Array Javascript

/**
 * Shuffles array in place.
 * @param {Array} a items An array containing the items.
 */
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}


/**
 * Shuffles array in place. ES6 version
 * @param {Array} a items An array containing the items.
 */
function shuffle(a) {
    for (let i = a.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [a[i], a[j]] = [a[j], a[i]];
    }
    return a;
}

var myArray = ['1','2','3','4','5','6','7','8','9'];
shuffle(myArray);
Blue Bear

Jawaban yang mirip dengan “JavaScript Acak Sortir Random”

Pertanyaan yang mirip dengan “JavaScript Acak Sortir Random”

Lebih banyak jawaban terkait untuk “JavaScript Acak Sortir Random” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya