Skip to content

Commit c82a989

Browse files
committed
feat(JS) handle exception using try, catch when code fails
1 parent 7d1ff60 commit c82a989

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

index.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ btns.forEach((btn) => {
2121

2222
if (buttonValue === "(") {
2323
let isOpenparenthesis = true;
24-
for(let i = data.length -1; i >= 0; i--) {
25-
if(/^\d$/.test(data[i])) {
26-
console.log( "last element in array " + data[i])
24+
for (let i = data.length - 1; i >= 0; i--) {
25+
if (/^\d$/.test(data[i])) {
26+
console.log("last element in array " + data[i]);
2727
isOpenparenthesis = false;
2828
break;
29-
} if (data[i] === ")") {
29+
}
30+
if (data[i] === ")") {
3031
isOpenparenthesis = false;
31-
break
32-
} if (/[\/*\-+]/.test(data[i])) {
33-
break
34-
32+
break;
33+
}
34+
if (/[\/*\-+]/.test(data[i])) {
35+
break;
3536
}
3637
}
37-
if(isOpenparenthesis) {
38+
if (isOpenparenthesis) {
3839
data.push("(");
3940
}
4041
screen.innerText = data.join("");
41-
4242
}
4343

4444
if (buttonValue === ")") {
@@ -103,14 +103,15 @@ btns.forEach((btn) => {
103103
}
104104

105105
if (buttonValue === "=") {
106-
// debugger
107-
let result = eval(data.join(""));
108-
console.log(result);
109-
console.log(data);
110-
data = [];
111-
data.push(result);
112-
screen.innerText = result;
113-
console.log(result);
106+
try {
107+
let result = eval(data.join(""));
108+
console.log(result);
109+
data = [];
110+
data.push(result);
111+
screen.innerText = result;
112+
} catch (e) {
113+
screen.innerText = e.name;
114+
}
114115
}
115116

116117
if (buttonValue === "%") {

0 commit comments

Comments
 (0)