We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 10 Days of Javascript
  4. Day 3: Throw

Day 3: Throw

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics
  1. Prepare
  2. Tutorials
  3. 10 Days of Javascript
  4. Day 3: Throw
Exit Full Screen View
  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial
  • Topics

Objective

In this challenge, we practice using throw and catch statements to work with custom error messages.

Task

Complete the isPositive function below. It has one integer parameter, . If the value of is positive, it must return the string YES. Otherwise, it must throw an Error according to the following rules:

  • If is , throw an Error with Zero Error.
  • If is negative, throw an Error with Negative Error.

Input Format

Locked stub code in the editor reads the following input from stdin and passes each value of to the function as an argument:
The first line is an integer, , denoting the number of times the function will be called with some .
Each line of the subsequent lines contains an integer denoting some .

Constraints

Output Format

If the value of is positive, the function must return the string YES. Otherwise, it must throw an Error according to the following rules:

  • If is , throw an Error with Zero Error.
  • If is negative, throw an Error with Negative Error.

Sample Input 0

3
1
2
3

Sample Output 0

YES
YES
YES

Explanation 0

Each of the given values is positive, so we return YES each time. The value returned during each function call is printed on a new line by locked stub code in the editor.

Sample Input 1

3
2
0
6

Sample Output 1

YES
Zero Error
YES

Explanation 1

Locked stub code in the editor makes the following three calls to the isPositive function:

  1. isPositive(2): This returns YES because is positive.
  2. isPositive(0): Because , we throw an Error with Zero Error. This is caught by the locked stub code and the value of its is printed.
  3. isPositive(6): This returns YES because is positive.

Sample Input 2

2
-1
20

Sample Output 2

Negative Error
YES

Explanation 2

Locked stub code in the editor makes the following two calls to the isPositive function:

  1. isPositive(-1): Because , we throw an Error with Negative Error. This is caught by the locked stub code and the value of its is printed.
  2. isPositive(20): This returns YES because is positive.
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy