Top Array Leetcode Problems Javascript Function