-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
195 lines (166 loc) · 5.53 KB
/
script.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// to get access to the id "history-value"
function getHistory()
{
return document.getElementById("history-value").innerText;
}
// to print the history value
function printHistory(num)
{
document.getElementById("history-value").innerText=num;
}
// to get access to the id "output-value"
function getOutput()
{
return document.getElementById("output-value").innerText;
}
// to print the output value
function printOutput(num)
{
// if value is empty, it sets it to empty, instead of printing 0
if(num=="")
{
document.getElementById("output-value").innerText=num;
}
else
{
document.getElementById("output-value").innerText=getFormattedNumber(num);
}
}
// to read the output comma separated to make readability easy
function getFormattedNumber(num)
{
// in case of negative number as a output we will return ""
if(num=="-")
{
return "";
}
var n=Number(num);
var value=n.toLocaleString("en");
return value;
}
// this will convert my comma separated back to the original number
function reverseNumberFormat(num)
{
return Number(num.replace(/,/g,''));
}
// Now we will get back to the operations for the operators
var operator=document.getElementsByClassName("operator");
for(var i=0;i<operator.length;i++)
{
// this will help us give an event if we click the operators
operator[i].addEventListener('click',function(){
// if we click on clear, both the history and output gets cleared
if(this.id=="clear")
{
printHistory("");
printOutput("");
}
// for backspace, commas should not be there so we get the number and convert it to string
else if(this.id=="backspace")
{
var output=reverseNumberFormat(getOutput()).toString();
// we will remove the last character using substring function
if(output) // if output has a value
{
output=output.substr(0,output.length-1);
printOutput(output);
}
}
// for all the operators like +,-,*,/,....
else
{
var output=getOutput();
var history=getHistory();
if(output=="" && history!="")
{
// if the last character is an operator
if(isNaN(history[history.length-1]))
{
history=history.substr(0,history.length-1);
}
}
// the operators does not work if the output is empty
// so lets first check the output is not empty
if(output!="" || history!="")
{
// if output is empty but history is not empty then output must be set to empty value
output=output==""?
output:reverseNumberFormat(output);
// in the calculator, when a operator is clicked the output value is fiest added to the history
history=history+output;
// if the user clicks in the "=" operator then the history will be evaluated
if(this.id=="=")
{
// this will evaluate the history
var result=eval(history);
// we will print the result in the output
printOutput(result);
// the history gets empty
printHistory("");
}
// if we click on other operator except the "="
else
{
// the operators gets added to the history and output is set to empty
history=history+this.id;
printHistory(history);
printOutput("");
}
}
}
})
}
// Now we will get back to the operations for the numbers
var number=document.getElementsByClassName("number");
for(var i=0;i<number.length;i++)
{
// this will help us give an event if we click the operators
number[i].addEventListener('click',function(){
var output=reverseNumberFormat(getOutput());
// if output is a number
if(output!=NaN)
{
output=output+this.id;
printOutput(output);
}
});
}
var microphone = document.getElementById('microphone');
microphone.onclick=function()
{
microphone.classList.add("record");
var recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition || window.mozSpeechRecognition || window.msSpeechRecognition)();
recognition.lang = 'en-US';
recognition.start();
operations = {"plus":"+",
"minus":"-",
"multiply":"*",
"multiplied":"*",
"divide":"/",
"divided":"/",
"reminder":"%"}
recognition.onresult = function(event)
{
var input = event.results[0][0].transcript;
for(property in operations)
{
input= input.replace(property, operations[property]);
}
document.getElementById("output-value").innerText = input;
setTimeout(function()
{
evaluate(input);
},2000);
microphone.classList.remove("record");
}
}
function evaluate(input){
try{
var result = eval(input);
document.getElementById("output-value").innerText = result;
}
catch(e){
console.log(e);
document.getElementById("output-value").innerText = "";
}
}