Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js how to shuffle array algoritm. The Fisher-Yates algorith

// @ts-check

// The Fisher-Yates algorith

(() => {
  let arr = [1, 2, 3, 4, 5];

  const shuffleArray = (array) => {
    for (let i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      const temp = array[i];
      array[i] = array[j];
      array[j] = temp;
    }
  };

  shuffleArray(arr);
  console.log(arr);

  shuffleArray(arr);
  console.log(arr);

  shuffleArray(arr);
  console.log(arr);
})();
Comment

PREVIOUS NEXT
Code Example
Javascript :: create user controller 
Javascript :: autonumeric stimulus 
Javascript :: javascript code to run colab in background 
Javascript :: vite build output destination change 
Javascript :: FILTER METHOD. IMPORTANT 
Javascript :: laravel , json Why it shows Cannot access offset of type string on string error 
Javascript :: how to hide javascript code from client 
Javascript :: javascript Vue Component Loading Before Vuex Data Is Set 
Javascript :: how do i set CORS policy for nodejs sever 
Javascript :: Setting the default value in the drop down list in AngularJS 
Javascript :: angularjs No alignment and missing item in a vertical menu 
Javascript :: Getting PointerEvent instead of jQuery.Event on ng-click in AngularJS 
Javascript :: Angular Frontend - How do I change a value I got from backend in frontend 
Javascript :: convert base64 formatted data to image using AngularJs 
Javascript :: Edit parameter in variable with increment/decrement box and save it 
Javascript :: Page Pre loader not removing 
Javascript :: js generate pnh 
Javascript :: fields filtering in api from express 
Javascript :: parse json keep the order 
Javascript :: Sequelize conditional shorthands 
Javascript :: assign single value to multiple variables in React js Or javacript 
Javascript :: code with mosh swipable react native not working 
Javascript :: What is an array? Is it static or dynamic in Javascript 
Javascript :: force browser reload page from server javascript 
Javascript :: miragejs createServer timing 
Javascript :: push replacement getx 
Javascript :: Toggle child element onclick of parent element 
Javascript :: jQuery mobile anchor link on the same page 
Javascript :: javascript check if array has at least one true value 
Javascript :: react creating function to call API in app: calling APIs after render w error message 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =