-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_throw.js
More file actions
42 lines (35 loc) 路 846 Bytes
/
Copy path10_throw.js
File metadata and controls
42 lines (35 loc) 路 846 Bytes
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
34
35
36
37
38
39
40
41
42
/**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 message with Zero Error.
If a is negative, throw an Error message with Negative Error */
function isPositive(a) {
if(a > 0){
var s = "YES";
}
else{
try{
if(a == 0){
throw "error2";
}
else if(a < 0){
throw "error1";
}
}
catch(e){
if(e == "error1"){
return ("Negative Error")
}
else if(e == "error2"){
return ("Zero Error")
}
}
}
return s;
}
// With Ternery operator
function isPositive2(a) {
return a<0 ? "Negative Error":a==0 ? "Zero Error":"YES";
}
console.log(isPositive(-5));