-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathday3-throw.js
More file actions
33 lines (28 loc) · 1.25 KB
/
Copy pathday3-throw.js
File metadata and controls
33 lines (28 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
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.
*/
function isPositive(a) {
if (a > 0) {
return "YES";
} else if (a == 0) {
throw new Error("Zero Error");
} else {
throw new Error("Negative Error");
}
}