Day 3: Try, Catch, and Finally in Javascript | 10 Days Of JavaScript Solutions | HackerRank Programming Solutions [💯Correct]

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, 10 Days Of JavaScript. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Day 3: Try, Catch, and Finally in JavaScript-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To JavaScript

JavaScript is a lightweight, cross-platform, and interpreted scripting language. It is well-known for the development of web pages, many non-browser environments also use it. JavaScript can be used for Client-side developments as well as Server-side developments. JavaScript contains a standard library of objects, like Array, Date, and Math, and a core set of language elements like operators, control structures, and statements.

Advantages of JavaScript

The merits of using JavaScript are −

  • Less server interaction − You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.
  • Immediate feedback to the visitors − They don’t have to wait for a page reload to see if they have forgotten to enter something.
  • Increased interactivity − You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.
  • Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.

Link for the ProblemDay 3: Try, Catch, and Finally – Hacker Rank Solution

Day 3: Try, Catch, and Finally – Hacker Rank Solution

Problem:

Objective

In this challenge, we learn about strings and exceptions. Check out the attached tutorials for more details.

Task

Complete the reverseString function; it has one parameter, . You must perform the following actions:

  1. Try to reverse string  using the splitreverse, and join methods.
  2. If an exception is thrown, catch it and print the contents of the exception’s  on a new line.
  3. Print  on a new line. If no exception was thrown, then this should be the reversed string; if an exception was thrown, this should be the original string.

Input Format

Locked stub code in the editor reads variable  from stdin and passes it to the function.

Output Format

You must write two print statements using console.log():

  1. Print the contents of a caught exception’s  on a new line. If no exception was thrown, this line should not be printed.
  2. Print  on a new line. If no exception was thrown, then this should be the reversed string; if an exception was thrown, this should be the original string.

Sample Input 0

"1234"

Sample Output 0

4321

Explanation 0

 is a string type, so it can be reversed without throwing an exception. Thus, we print the reversed value, 4321, as our answer.

Sample Input 1

Number(1234)

Sample Output 1

s.split is not a function
1234

Explanation 1

 is not a string type, so it can’t be reversed using string functions. When we try to reverse it anyway, it throws an exception. We then catch the exception and print its , which is s.split is not a function. Next, we finally print  which, because it wasn’t able to be reversed, is Number(1234).

Day 3: Try, Catch, and Finally – Hacker Rank Solution
function getSecondLargest(nums) {
  let firstLargestNum = 0;
  let secondLargestNum = 0;

  for (let i = 0; i  firstLargestNum) {
      secondLargestNum = firstLargestNum;
      firstLargestNum = nums[i];
    }
    if (nums[i] > secondLargestNum && nums[i] 
		

Leave a Comment

Scroll to Top