JSFiddle - React, Tailwind, and code Playground

by zachpainter77

HTML

<span id="output"></span>

JavaScript

function findNextPageIndex(arr, currentIndex) {
    arr = arr.sort(function (a, b) { return b - a; });
       
    return arr.reduceRight(function (prev, curr) { return prev > curr ? prev : --curr; }, currentIndex);
}

document.getElementById('output').innerHTML(findNextPageIndex([2,4,5],0));