-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
181 lines (157 loc) · 5.79 KB
/
script.js
File metadata and controls
181 lines (157 loc) · 5.79 KB
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
//An alias to the view element
const view = document.getElementById('view-in')
const answerView = document.getElementById('answer')
//Adding click event listerners to all buttons
const buttons = document.querySelectorAll('button');
for(let i = 0; i < buttons.length; i++) {
let elem = buttons[i];
elem.addEventListener('click', function() {
addThis(elem.textContent)
})
}
//Check if provided value is valid equation
function checkValue(equation) {
const REGEX = /\-?([0-9]+)\.?([0-9]+)?/g, MATCHED_VALUE = equation.match(REGEX)
if (MATCHED_VALUE) return equation === MATCHED_VALUE[0]
}
//Get the operators present in the equation
function getOperators(equation) {
const OPERATOR_REGEX = /(\÷|\x|\-|\+)/g
return equation.match(OPERATOR_REGEX)
}
//Get the index of the last operator in the equation
function getOperatorIndex(equation) {
//Collect all operators from the equation
const MATCHED_VALUE = getOperators(equation)
//Pop the last operator from the matched values and get it's index from the right of the equation
if (MATCHED_VALUE) return equation.lastIndexOf(MATCHED_VALUE.pop())
return NaN
}
//Get the RHS (right-hand-side) value of the current equation in view
function getRHS(equation) {
//slice the equation from the last operator to the end of the string, giving the RHS
return equation.slice(getOperatorIndex(equation) + 1)
}
function getLHS(equation) {
return equation.slice(0, equation.lastIndexOf(getRHS(equation)))
}
//Add comma's whereever required to the current equation
function addComma(equation, view) {
//equation = stripComma(equation)
if(checkValue(stripComma(equation))) {
view.innerHTML = rewrite(equation)
}
else {
const RHS = getRHS(equation), LHS = getLHS(equation)
view.innerHTML = LHS + rewrite(RHS)
}
function rewrite(equation) {
equation = stripComma(equation)
if(equation.length > 3 && equation.indexOf('.') === -1) {
return breakString(equation).join(',')
}
else return equation;
}
function breakString(string) {
const REVERSE_EQUATION = string.split('').reverse().join('')
let arr = [], count = 0
for(let i = 3; i < REVERSE_EQUATION.length; i += 3){
arr.push(REVERSE_EQUATION.slice(i-3, i))
count = i
}
arr.push(REVERSE_EQUATION.slice(count))
return arr.map(function (e) { return e.split('').reverse().join('')}).reverse()
}
}
function updateChar(equation, oldChar, newChar) {
return equation.split('').map(function(elem) {
return elem === oldChar ? newChar : elem
}).join('')
}
function stripComma(equation) {
return updateChar(equation, ',', '')
}
//Add typed/clicked value to the equation
function addThis(value) {
let EQUATION = view.innerHTML
if(value === '*') value = 'x'
if(value === '/') value = '÷';
//If view is empty and the key pressed is '-', just update the view with '-'
//This is being done because I couldn't figure out how to select the '-' at the start of an equation on it's own, when it's optional
if(!EQUATION && value === '-') updateEquation(value, "update")
//If the key pressed is an operator, do the following
//Make sure view is not empty and check if the key pressed is an operator
else if(EQUATION && getOperators(value)){
//Make sure the equation till now is a valueid number. If so, just update the view with the operator
if(checkValue(EQUATION)) updateEquation(value, "update")
//Check if the last character is an operator
else if (getOperators(EQUATION.split('').pop())) {
const NEW_EQUATION = EQUATION.split('').map(function(elem, index) {
return index === EQUATION.length - 1 ? value : elem
}).join('')
updateEquation(NEW_EQUATION, "rewrite")
}
else if (getRHS(EQUATION)){
updateEquation(value, "update")
}
}
//Make sure equation is not empty and then check if the index of the last opeator is less than or equal to the length of equation
else if (EQUATION && getOperatorIndex(EQUATION) <= EQUATION.length) {
EQUATION += value
const RHS = getRHS(EQUATION)
if(checkValue(stripComma(RHS))) updateEquation(value, "update")
}
//
else {
EQUATION += value
if(checkValue(stripComma(EQUATION))){
updateEquation(value, "update")
}
}
}
//Add keybord listerners event to the window
window.addEventListener("keydown", function (e) {
const key = e.key, keys = ['/', '*', '-', '+', '.']
if(!isNaN(key) || keys.indexOf(key) !== -1) addThis(key)
else if(key === 'Enter') equal()
else if(key === 'Delete') clr()
else if(key === 'Backspace') backspace()
})
//Clear the view
function clr() {
updateEquation("", "rewrite")
}
//Remove the last character from the view
function backspace() {
const eq = view.innerHTML
updateEquation(eq.slice(0, eq.length - 1), "rewrite")
}
//Evalurate the current equation in view
function equal() {
const eq = updateChar(updateChar(stripComma(view.innerHTML), 'x', '*'), '÷', '/');
//eq = updateChar(stripComma(view.innerHTML), '÷', '/');
updateEquation(eval(eq), "rewrite")
}
function autoAnwer() {
if (getOperatorIndex(view.innerHTML) < view.innerHTML.length - 1){
answerView.innerHTML = eval(updateChar(updateChar(stripComma(view.innerHTML), 'x', '*'), '÷', '/'))
addComma(answerView.innerHTML, answerView)
}
else answerView.innerHTML = ""
}
//Update the font size of the equation in view as it's changed
function updateFontSize() {
const len = stripComma(view.innerHTML).length
if ( len < 10) view.style.fontSize = '40px'
else if (len < 15) view.style.fontSize = '30px'
else if (len < 20) view.style.fontSize = '20px'
}
//Update the equation in the view
function updateEquation(EQUATION, action) {
// updateChar(EQUATION, '/', '÷');
if(action === "rewrite") view.innerHTML = EQUATION;
if(action === "update") view.innerHTML += EQUATION
addComma(view.innerHTML, view)
updateFontSize()
autoAnwer()
}