forked from mohsinkd786/js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate.js
64 lines (58 loc) · 1.54 KB
/
calculate.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
const calculate = () =>{
//console.log(`calculate() called`);
const first = parseInt(document.getElementById('first').value);
const next = parseInt(document.getElementById('next').value);
const operand = document.getElementById('operand').value;
let result = 0;
const calculator = new Calculator();
switch(operand){
case 'add':
//result = `Sum is , ${calculator.add(first,next)}`;
// making static method calls
result = `Sum is , ${StaticCalculator.add(first,next)}`;
break;
case 'diff':
result = `Difference is , ${calculator.subtract(first,next)}`;
break;
case 'mul':
result = `Product is , ${calculator.multiply(first,next)}`;
break;
case 'div':
let esCal = new ESCalculator();
result = `Division is , ${esCal.division(first,next)}`;
break;
default:
result = 0;
}
// end of switch
const resultant = document.getElementById('result');
// set the calculated value
resultant.innerHTML = result;
}
// calculator class
class Calculator{
// addition
add(first,next){
return first + next;
}
// difference
subtract(first,next){
return first - next;
}
//product
multiply(first,next){
return first * next;
}
}
//
class StaticCalculator{
static add(first,next){
return first + next;
}
}
// ES6 style
const ESCalculator = class {
division(first,next){
return first / next;
}
}