-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrectangle.js
More file actions
48 lines (42 loc) · 1.09 KB
/
rectangle.js
File metadata and controls
48 lines (42 loc) · 1.09 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
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
if (err) {
console.log(err.message);
} else {
// console.log(`- length: ${length}, width: ${width}`)
console.log(`- The area : ${result.area()}`);
console.log(`- The perimeter : ${result.perimeter()}`);
}
}
// Test
rectangle(2, 4, sloveRectangle);
rectangle(3, 5, sloveRectangle);
rectangle(-3, 5, sloveRectangle);