-
-
Notifications
You must be signed in to change notification settings - Fork 858
Expand file tree
/
Copy pathcalculator.js
More file actions
29 lines (23 loc) · 672 Bytes
/
calculator.js
File metadata and controls
29 lines (23 loc) · 672 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
// Calculator module that performs basic arithmetic operations
function Calculator(num1, num2) {
function sum() {
return num1 + num2;
}
function difference() {
return num1 - num2;
}
function product() {
return num1 * num2;
}
function dividend() {
if (num2 === 0) return "Error: Division by zero!";
return Math.floor(num1 / num2);
}
return { sum, difference, product, dividend };
}
// Example usage
const calc12And5 = Calculator(12, 5);
console.log("Sum:", calc12And5.sum());
console.log("Difference:", calc12And5.difference());
console.log("Product:", calc12And5.product());
console.log("Dividend:", calc12And5.dividend());