Answers for "typescript built in shuffle function"

13

javascript randomly shuffle 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"]
Posted by: Guest on July-24-2019
0

javascript fisher yates shuffle mdn

function shuffle(array) {
  for (let i = array.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1)); // random index from 0 to i

    // swap elements array[i] and array[j]
    // we use "destructuring assignment" syntax to achieve that
    // you'll find more details about that syntax in later chapters
    // same can be written as:
    // let t = array[i]; array[i] = array[j]; array[j] = t
    [array[i], array[j]] = [array[j], array[i]];
  }
}
Posted by: Guest on December-11-2019

Code answers related to "typescript built in shuffle function"

Code answers related to "Javascript"

Browse Popular Code Answers by Language