In this HackerRank Day 3: Arrays 10 days of javascript problem solution Complete the getSecondLargest function in the editor below.

getSecondLargest has the following parameters:

int nums[n]: An array of integers

HackerRank Day 3: Arrays problem solution


Problem solution in JavaScript programming.

/**
*   Return the second largest number in the array.
*   @param {Number[]} nums - An array of numbers.
*   @return {Number} The second largest number in the array.
**/
function getSecondLargest(nums) {
    let max = Math.max(...nums)
    let max2 = -Infinity
    nums.forEach(e => e < max && e > max2 ? max2 = e : null)
    return max2
}