-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·139 lines (130 loc) · 5.46 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//JavaScript codes
//Constructor:begin
function result(x,y,operation){
this.x=x;//x-variable to store the first value
this.y=y;//y-variable to store the second value
this.operation=operation;//operation variable is to store the type of the operation
}
//constructor:end
//...............................................................................
var esta= new result(0,0,"empty");//creat an object
//...............................................................................
//method:values:begin:this method is to disply the numbers on the text field
function values(x) {
document.calculator.output.value += x;
}
//the below functions are to do the opperations. The name od the method refers to
//the rules
//............................................................................
//Method:Addition:begin
function Addition(){
esta.x = document.calculator.output.value;//store the first value in the object
esta.operation = "addition";//store the name of the operation in the objest
document.getElementById("output").value = "";
document.calculator.output.value = ' - ';
document.getElementById("output").value = "";//clear the screan
return (esta.x, esta.operation);//retun the object vlues
}
//Method:Addition:end
//............................................................................
//Method:Minus:begin
function Minus() {
esta.x = document.calculator.output.value;//store the first value in the object
esta.operation = "minus";//store the name of the operation in the objest
document.getElementById("output").value = "";
document.calculator.output.value = ' - ';
document.getElementById("output").value = "";//clear the screan
return (esta.x, esta.operation);//retun the object vlues
}
//Method:Minus:end
//..................................................................
//Method:Division:begin
function Division() {
esta.x = document.calculator.output.value;//store the first value in the object
esta.operation = "division";//store the name of the operation in the objest
document.getElementById("output").value = "";
document.calculator.output.value = ' / ';
document.getElementById("output").value = "";//clear the screan
return (esta.x, esta.operation);//retun the object vlues
}
//Method:Division:end
//..................................................................
//Method:Sign:begin
function Multiply() {
esta.x = document.calculator.output.value;//store the first value in the object
esta.operation = "multiply";//store the name of the operation in the objest
document.getElementById("output").value = "";
document.calculator.output.value = ' * ';
document.getElementById("output").value = "";//clear the screan
return (esta.x, esta.operation);//retun the object vlues
}
//Method:Sign:end
//..................................................................
//Method:Sign:begin
function Sign() {
//multiply by (-1) to change the sign to the opposite one
document.calculator.output.value = document.calculator.output.value * (-1);
}
//Method:Sign:end
//..................................................................
//Method:Mod:begin
function Mod() {
esta.x = document.calculator.output.value;
esta.operation = "mod";//store the name of the operation in the objest
document.getElementById("output").value = "";//clear the screan
document.calculator.output.value = ' % ';
document.getElementById("output").value = ""; //clear the screan
return (esta.x, esta.operation); //retun the object vlues
}
//Method:Mod:end
//..................................................................
//Method:Clear:begin
function Clear() {
document.getElementById("output").value = "";//clear the screan
//set the object attribute to zero
esta.x = 0;
esta.y = 0;
return (esta.x, exta.y);
}
//Method:Mod:end
//..................................................................
//Method:equal:begin
function equal() {//in this method where we do the arthmitic operations
esta.y = document.calculator.output.value;//store the scond value in the object
if (esta.operation == "addition") {
document.calculator.output.value = ' = ';
document.getElementById("output").value = "";
final = Number(esta.x) + Number(esta.y);//final:it is the final answer
document.calculator.output.value = final;//display the value
} else {
if (esta.operation == "minus") {//if my operation is minus, subtract y from x
document.calculator.output.value = ' = ';
document.getElementById("output").value = "";
final = Number(esta.x) - Number(esta.y);
document.calculator.output.value = final;
} else {
if(esta.operation == "division") {
document.calculator.output.value = ' = ';
document.getElementById("output").value = "";
final = Number(esta.x) / Number(esta.y);
document.calculator.output.value = final;
} else {
if(esta.operation == "mod") {
document.calculator.output.value = ' = ';
document.getElementById("output").value = "";
final = Number(esta.x) % Number(esta.y);
document.calculator.output.value = final;
} else {
if(esta.operation == "multiply") {
document.calculator.output.value = ' = ';
document.getElementById("output").value = "";
final = Number(esta.x) * Number(esta.y);
document.calculator.output.value = final;
}
}
}
}
}
}
//Method:equal:end
//..................................................................