Sorted Permutation Rank Leetcode Problems Javascript