- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Alternative shuffle in JavaScript
Alternative Shuffle
An alternatively shuffled array in JavaScript is an array of Numbers in which numbers are indexed such that greatest number is followed by the smallest element, second greatest element is followed by second smallest element and so on.
For example: If the input array is −
const arr = [11, 7, 9, 3, 5, 1, 13];
Then the output should be &minus
const output = [13, 1, 11, 3, 9, 5, 7];
Example
Following is the code −
const arr = [11, 7, 9, 3, 5, 1, 13]; const sorter = (a, b) => a - b; const alternateShuffle = (arr) => { const array = arr .slice() .sort(sorter); array.sort((a, b) => a-b); for(let start = 0; start < array.length; start += 2){ array.splice(start, 0, array.pop()); } return array; }; console.log(alternateShuffle(arr));
Output
This will produce the following output in console −
[ 13, 1, 11, 3, 9, 5, 7 ]
- Related Articles
- What is Fisher–Yates shuffle in JavaScript?
- How to do Butterfly Shuffle in JavaScript?
- Alternative sorting of an array in JavaScript
- How to randomize (shuffle) a JavaScript array?
- How to shuffle an array in a random manner in JavaScript?
- Fetch alternative even values from a JavaScript array?
- Finding sum of alternative elements of the array in JavaScript
- Checking the intensity of shuffle of an array - JavaScript
- shuffle() function in PHP
- Alternative Sorting in C++
- Which is the JavaScript RegExp to find any alternative text?
- Shuffle Array Contents
- Shuffle vs random_shuffle in C++
- Shuffle an Array in Python
- Alternative of click() in Selenium

Advertisements