Last active
December 22, 2025 14:58
-
-
Save wperron/74bb5557740b4b6a1e44ebe3caa47b5d to your computer and use it in GitHub Desktop.
Does this array alternate?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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