-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrectangle.js
39 lines (33 loc) · 892 Bytes
/
rectangle.js
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
/*
Complete the the function named "sloveRectangle" so it can print as follows.
$ rectangle(2, 4, sloveRectangle);
- The area: 8 - The perimeter: 12
$ rectangle(3, 5, sloveRectangle);
- The area: 15 - The perimeter: 16
$ rectangle(-3, -5, sloveRectangle);
- Error : Rectangle dimensions should be greater than zero
*/
function rectangle(length, width, callback) {
try {
if (length < 0 || width < 0) {
throw new Error("Rectangle dimensions should be greater than zero");
}
callback(null, {
perimeter: function () {
return 2 * (length + width);
},
area: function () {
return length * width;
}
});
} catch (error) {
callback(error, null);
}
}
function sloveRectangle(err, result) {
// To be implemented
}
// Test
rectangle(2, 4, sloveRectangle);
rectangle(3, 5, sloveRectangle);
rectangle(-3, 5, sloveRectangle);