Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is it correct to use JavaScript Array.sort() method for shuffling?
Yes, you can use the JavaScript Array.sort() method for shuffling. Let’s see how
Example
function shuffleDisplay(arr) {
var tmp, current;
// calculating length
var top = arr.length;
if(top) while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = arr[current];
arr[current] = arr[top];
arr[top] = tmp;
}
return arr;
}Advertisements