Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

The Fisher Yates Method

The points.sort() method depicted in the example above may not yield accurate results, as it could favor certain numbers over others.

One of the most commonly used correct methods is known as the Fisher-Yates shuffle, which has been employed in data science since as early as 1938!

In JavaScript, this method can be implemented as follows:

Example

const points = [40100152510];

for (let i = points.length –1; i > 0; i–) {
  let j = Math.floor(Math.random() * (i+1));
  let k = points[i];
  points[i] = points[j];
  points[j] = k;
}

Find the Lowest (or Highest) Array Value

Built-in functions for identifying the maximum or minimum value in an array are not available.To determine the lowest or highest value, you have three alternatives:

  • Sort the array and access the first or last element.
  • Utilize Math.min() or Math.max().
  • Craft a custom function.