Day 3: Throw

Day 3: Throw

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, a. If the value of a is positive, it must return the string YES. Otherwise, it must throw an Error according to the following rules:

  • If a is 0, throw an Error with message = Zero Error.
  • If a is negative, throw an Error with message = Negative Error.

Input Format

Locked stub code in the editor reads the following input from stdin and passes each value of a to the function as an argument:

The first line is an integer, n, denoting the number of times the function will be called with some a.

Each line i of the n subsequent lines contains an integer denoting some a.

Constraints

  • 1 <= n <= 5
  • -100 <= a <= 100

Output Format

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

  • If a is 0, throw an Error with message = Zero Error.
  • If a is negative, throw an Error with message = Negative Error.

Sample Input 0

1
2
3
4
3
1
2
3

##Sample Output 0

1
2
3
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

1
2
3
4
3
2
0
6

##Sample Output 1

1
2
3
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 2 is positive.
  2. isPositive(0): Because a = 0, we throw an Error with message = Zero Error. This is caught by the locked stub code and the value of its message is printed.
  3. isPositive(6): This returns YES because 6 is positive.

Sample Input 2

1
2
3
2
-1
20

##Sample Output 2

1
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 a = -1, we throw an Error with message = Negative Error.
    This is caught by the locked stub code and the value of its message is printed.
  2. isPositive(20): This returns YES because 20 is positive.



Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* 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) {
let result = 'YES';

if (a < 0) {
throw { message: 'Negative Error' };
} else if (a === 0) {
throw { message: 'Zero Error' };
}

return result;
}

Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×