Skip to content

Commit f68c879

Browse files
Added parentheses check
- Parentheses check - Refactored char check - Warning tooltip updates based on failed checks
1 parent 41b154d commit f68c879

3 files changed

Lines changed: 21 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Note: _The order of this list might not match the order these features will be a
6666

6767
_Release date: ???????????????????????_
6868

69-
_Work Time: 5h_
69+
_Work Time: 5h 40min_
7070

7171
-
7272

pages/common.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ var textfield = document.querySelector('#expression');
192192
var result = document.querySelector('#result num');
193193
var icons = document.querySelectorAll('#result icon > *');
194194
var button = document.querySelector('#calc button');
195+
var errors = {
196+
char: 'Only numbers or +-*/(). characters are allowed.',
197+
par: 'Some parentheses are missing.'
198+
}
199+
195200

196201
function openEditor() {
197202
editor.style.display = 'grid';
@@ -230,14 +235,14 @@ editor.addEventListener('keyup', function (event) {
230235
textfield.addEventListener('input', checkErrors);
231236

232237
function checkErrors(event) {
233-
var inp = event.target.value.toString();
234-
235-
// check allowed characters => 0123456789.+-*/()
236-
var test = inp.match(/[0-9|\+|\-|\*|\/|(|)|\.]+/);
237-
if (!test) test = [""];
238-
239-
// if length of input = characters matched expression is valid
240-
if (inp.length === test[0].length) {
238+
var inp = event.target.value.toString().split('');
239+
// check if every character is allowed => 0123456789.+-*/()
240+
var charTest = inp.every(char => char.match(/[0-9|\+|\-|\*|\/|(|)|\.]/));
241+
// check if parentheses match
242+
var parTest = inp.filter(char => char == '(').length == inp.filter(char => char == ')').length;
243+
244+
// update icon and button
245+
if (charTest && parTest) {
241246
button.disabled = false;
242247
showIcon(0);
243248
} else {
@@ -250,7 +255,11 @@ function checkErrors(event) {
250255
icons[index].classList.remove('invisible');
251256
}
252257

253-
// check if parentheses match
254-
258+
// update warning tooltip
259+
var tooltip = [];
260+
if (!charTest) tooltip.push(errors.char);
261+
if (!parTest) tooltip.push(errors.par);
262+
console.log(tooltip)
263+
icons[1].querySelector('span').innerText = tooltip.join('\n');
255264
}
256265

pages/cylinder.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ <h4>Expression editor</h4>
138138
<span>Expression OK</span>
139139
</i>
140140
<i class="fi-rr-exclamation invisible">
141-
<span>Only numbers or +-*/() characters are allowed. Some parentheses are missing.</span>
141+
<span></span>
142142
</i>
143143
</icon>
144144
</span>

0 commit comments

Comments
 (0)