Solutions
HackerRank
HackerRank
  • Home
  • 👨🏻‍💻 Profile
  • Prepare
    • Linux Shell
      • Bash
    • Python
      • Introduction
    • SQL
      • Basic Select
      • Advanced Select
      • Aggregation
      • Basic Join
  • Tutorials
    • 10 Days of Javascript
      • Day 0
      • Day 1
      • Day 2
      • Day 3
  • Certify
    • C# (Basic)
    • JavaScript (Basic)
    • SQL (Basic)
    • Rest API (Intermediate)
Powered by GitBook
On this page
  • Arrays
  • Try, Catch, and Finally
  • Throw

Was this helpful?

  1. Tutorials
  2. 10 Days of Javascript

Day 3

Arrays, Try, Catch, and Finally, Throw

PreviousDay 2NextC# (Basic)

Last updated 2 years ago

Was this helpful?

Arrays

/**
*   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");
}
LogoDay 3: Arrays | HackerRankHackerRank
LogoDay 3: Try, Catch, and Finally | HackerRankHackerRank
LogoDay 3: Throw | HackerRankHackerRank