← All Exercises
Compute Roving tabindex Moves for an ARIA Tab List
intermediatejavascript
Background
The tabs pattern uses one tab stop for the active tab and arrow keys to move focus, so the index math must wrap and honor orientation.
Problem
Write nextTabIndex(state) that returns the index a tab list should focus after an Arrow, Home, or End key, following the WAI-ARIA tabs pattern for horizontal and vertical lists.
Input
A JSON string with activeIndex, key, count, and orientation.
Output
The next active index as a number, or -1 for an empty tab list.
Sample tests
Sample 1
Input: {"activeIndex":2,"key":"ArrowRight","count":3,"orientation":"horizontal"}Expected: 0
Sample 2
Input: {"activeIndex":0,"key":"ArrowLeft","count":3,"orientation":"horizontal"}Expected: 2
Sample 3
Input: {"activeIndex":1,"key":"End","count":3,"orientation":"horizontal"}Expected: 2
Sample 4
Input: {"activeIndex":1,"key":"Tab","count":3,"orientation":"horizontal"}Expected: 1
Sample 5
Input: {"activeIndex":0,"key":"ArrowDown","count":4,"orientation":"vertical"}Expected: 1
Your solution
Learning objective
Implement WAI-ARIA tabs keyboard semantics with roving tabindex, orientation-aware arrows, and wrapping.