-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog-funcional-2.js
89 lines (76 loc) · 2.03 KB
/
prog-funcional-2.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var operation = {
'+' : function (n1,n2) {
return n1+n2;
},
'-': function (n1, n2) {
return n1 - n2;
},
'*': function (n1, n2) {
return n1 * n2;
},
'/': function (n1, n2) {
return n1 / n2;
},
'%': function (n1, n2) {
return n1 % n2;
}
}
function isOperatorValid (operator) {
return operation[operator] !== undefined;
}
function calculator (operator) {
if(isOperatorValid(operator)) {
return function(n1,n2) {
if (typeof n1 === 'number' || typeof n2 === 'number') {
return operation[operator](n1,n2);
}
return false;
};
}
return false;
}
function showOperationMessage(operator, n1, n2) {
return 'A operação '+n1+' '+operator+' '+n2+' =';
}
function showErrorMessage(operator) {
return 'Operação '+operator+' não permitida!';
}
var number1 = 0;
var number2 = 0;
var operationSignal;
operationSignal = '+';
var sum = calculator(operationSignal);
if (sum) {
number1 = 10;
number2 = 30;
console.log(showOperationMessage(operationSignal, number1, number2),sum(number1,number2));
} else {
console.log(showErrorMessage(operationSignal));
}
operationSignal = '-';
var subtraction = calculator(operationSignal);
if (subtraction) {
number1 = 40;
number2 = 30;
console.log(showOperationMessage(operationSignal, number1, number2), subtraction(number1, number2));
} else {
console.log(showErrorMessage(operationSignal));
}
operationSignal = '*';
var mult = calculator(operationSignal);
if (mult) {
number1 = 40;
number2 = 30;
console.log(showOperationMessage(operationSignal, number1, number2), mult(number1, number2));
} else {
console.log(showErrorMessage(operationSignal));
}
operationSignal = '$';
var error = calculator(operationSignal);
if (error) {
number1 = 40;
number2 = 30;
console.log(showOperationMessage(operationSignal, number1, number2), error(number1, number2));
} else {
console.log(showErrorMessage(operationSignal));
}