typestar

Fisher-Yates shuffle in JavaScript

Fisher-Yates, the shuffle that is actually uniform, unlike sorting by a random comparator.

function shuffle(items) {
  const out = [...items];
  for (let i = out.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [out[i], out[j]] = [out[j], out[i]];
  }
  return out;
}

Keywords and builtins used here

The run, in numbers

Lines
8
Characters to type
192
Tokens
82
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 55 seconds.

Type this snippet

Step 8 of 10 in Arrays, step 24 of 43 in Language basics.

← Previous Next →