10 Days of JavaScript (Day 3) HackerRank Solutions #10daysofjavascript

 DAY 3: Arrays

Solution >>>

function getSecondLargest(nums) {

  let firstLargestNum = 0;

  let secondLargestNum = 0;

  for (let i = 0; i < nums.length; i++) {

    if (nums[i] > firstLargestNum) {

      secondLargestNum = firstLargestNum;

      firstLargestNum = nums[i];

    }

    if (nums[i] > secondLargestNum && nums[i] < firstLargestNum) {

      secondLargestNum = nums[i];

    }

  }

  return secondLargestNum;

}




DAY 3: Try, Catch, and Finally

Solution >>>

function reverseString(s) {

  if (typeof s === "string") {

    console.log(

      s

        .split("")

        .reverse()

        .join("")

    );

  } else {

    console.log("s.split is not a function" + "\n" + s);

  }

}



DAY 3: Throw

Solution >>>

function isPositive(a) {

  if (a === 0) {

    throw Error("Zero Error");

  }

  if (a < 0) {

    throw Error("Negative Error");

  }

  return "YES";

}


Comments

Popular posts from this blog

Introduction to the Internet of Things and Embedded Systems (Week 2)

The Arduino Platform and C Programming (Week 1)