Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

What's The REAL Secret To First Date Success With a Woman? Click Here To Find Out

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

29,372 questions

38,322 answers

573 users

How to shuffle an array in JavaScript

1 Answer

0 votes
const 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]];
    }
}
 
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 19];
 
shuffleArray(array);
 
console.log(array);
 
 
 
 
/*
run:
 
[4, 19, 1, 6, 7, 9, 0, 8, 5, 2, 3]
 
*/

 

 


Protect Your Privacy - Download VPN


answered Jan 4 by avibootz

Related questions

1 answer 55 views
1 answer 16 views
1 answer 18 views
1 answer 13 views
13 views asked Jan 4 by avibootz
...