Skip to content

Instantly share code, notes, and snippets.

@wperron
Last active December 22, 2025 14:58
Show Gist options
  • Select an option

  • Save wperron/74bb5557740b4b6a1e44ebe3caa47b5d to your computer and use it in GitHub Desktop.

Select an option

Save wperron/74bb5557740b4b6a1e44ebe3caa47b5d to your computer and use it in GitHub Desktop.
Does this array alternate?
function alternates(arr) {
if (arr.length <= 2) return true;
let a = arr[0];
let b = arr[1];
for (let i = 2; i < arr.length; i++) {
if (arr[i] != a) return false;
[a, b] = [b, arr[i]];
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment