/**
* 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) {
// Complete the function
let firstLargest = 0;
let secondLargest = 0;
for (let i = 0; i < nums.length; i++){
if(nums[i] > firstLargest){
secondLargest = firstLargest;
firstLargest = nums[i];
}
if(nums[i] < firstLargest && nums[i] > secondLargest){
secondLargest = nums[i];
}
}
return secondLargest;
}
Try, Catch, and Finally
/*
* Complete the reverseString function
* Use console.log() to print to stdout.
*/
function reverseString(s) {
try{
s = s.split('').reverse().join('');
}
catch(e){
console.log(e.message);
}
finally{
console.log(s);
}
}
Throw
/*
* Complete the isPositive function.
* If 'a' is positive, return "YES".
* If 'a' is 0, throw an Error with the message "Zero Error"
* If 'a' is negative, throw an Error with the message "Negative Error"
*/
function isPositive(a) {
if(a > 0)
return "YES";
else if(a == 0)
throw new Error("Zero Error");
else
throw new Error("Negative Error");
}