mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-08 19:06:00 +00:00
* Get Bit: Make more terse * Power of two: Allowed 1 as a valid power of 2. Power of two: Removed unnecessary exception throwing. * Fisher Yates: Made more terse * Least Common Multiple: Fill undefined value * Greatest Common Divisor: Fill undefined value. Greatest Common Divisor: Make more terse.
16 lines
443 B
JavaScript
16 lines
443 B
JavaScript
/**
|
|
* @param {*[]} originalArray
|
|
* @return {*[]}
|
|
*/
|
|
export default function fisherYates(originalArray) {
|
|
// Clone array from preventing original array from modification (for testing purpose).
|
|
const array = originalArray.slice(0);
|
|
|
|
for (let i = (array.length - 1); i > 0; i -= 1) {
|
|
const randomIndex = Math.floor(Math.random() * (i + 1));
|
|
[array[i], array[randomIndex]] = [array[randomIndex], array[i]];
|
|
}
|
|
|
|
return array;
|
|
}
|