Skip to content

Commit f192ada

Browse files
authored
fixedUI (#434)
Fixed UI improvements
1 parent 0b4f5d3 commit f192ada

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

BasicCalculator/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8">
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<link rel="stylesheet" href="/style.css"/>
7+
<link rel="stylesheet" href="style.css"/>
88
<title>Calculator</title>
99
</head>
1010
<body>
@@ -24,6 +24,7 @@
2424
<input type="button" value="8" class="nums" onclick='getkey("8")' id="num-8">
2525
<input type="button" value="9" class="nums" onclick='getkey("9")' id="num-9">
2626
<input type="button" value="0" class="nums" onclick='getkey("0")' id="num-0">
27+
<input type="button" value="." class="nums" onclick='getDecimal()' id="num-decimal">
2728

2829
</div>
2930
<div class="btn-oprs">
@@ -38,6 +39,6 @@
3839
</div>
3940
</form>
4041
</div>
41-
<script type = "text/javascript" src="/script.js"></script>
42+
<script type = "text/javascript" src="script.js"></script>
4243
</body>
4344
</html>

BasicCalculator/script.js

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,30 @@ const getkey = (key)=>{
77
if(key == '='){
88
output_win.innerHTML = "INVALID OPERATION";
99
expression = "";
10+
firstVal = true;
1011
}
11-
1212
else{
1313
expression = key;
14-
output_win.innerHTML += key;
14+
output_win.innerHTML = key;
1515
firstVal = false;
1616
}
1717
}
1818
else{
1919
if(key != '='){
2020
expression += key;
21-
output_win.innerHTML += key;
21+
output_win.innerHTML = expression;
2222
}
2323
else{
2424
try{
25-
26-
output_win.innerHTML = eval(expression).toString();
25+
let result = eval(expression).toString();
26+
output_win.innerHTML = result;
27+
expression = result;
28+
firstVal = false;
2729
}
28-
2930
catch(err){
3031
output_win.innerHTML = "INVALID OPERATION";
3132
expression = "";
33+
firstVal = true;
3234
}
3335
}
3436
}
@@ -37,15 +39,48 @@ const getkey = (key)=>{
3739
const getClr = ()=>{
3840
output_win.innerHTML = "";
3941
expression = "";
42+
firstVal = true;
4043
}
4144

4245
const leftPar = ()=>{
4346
lp = document.getElementById('btn-leftPar').value;
44-
output_win.innerHTML += lp;
45-
expression += lp;
47+
if(firstVal){
48+
expression = lp;
49+
output_win.innerHTML = lp;
50+
firstVal = false;
51+
} else {
52+
expression += lp;
53+
output_win.innerHTML = expression;
54+
}
4655
}
4756
const rightPar = ()=>{
4857
rp = document.getElementById('btn-rightPar').value;
49-
output_win.innerHTML += rp;
50-
expression += rp;
58+
if(firstVal){
59+
expression = rp;
60+
output_win.innerHTML = rp;
61+
firstVal = false;
62+
} else {
63+
expression += rp;
64+
output_win.innerHTML = expression;
65+
}
5166
}
67+
68+
const getDecimal = ()=>{
69+
if(expression == "" || expression == undefined || firstVal){
70+
expression = "0.";
71+
output_win.innerHTML = "0.";
72+
firstVal = false;
73+
}
74+
else{
75+
76+
let lastOperatorIndex = Math.max(expression.lastIndexOf('+'), expression.lastIndexOf('-'),
77+
expression.lastIndexOf('*'), expression.lastIndexOf('/'),
78+
expression.lastIndexOf('('));
79+
let currentNumber = expression.substring(lastOperatorIndex + 1);
80+
81+
if(!currentNumber.includes('.')){
82+
expression += ".";
83+
output_win.innerHTML = expression;
84+
}
85+
}
86+
}

BasicCalculator/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
width: 50%;
5757
background-color:azure;
5858
display:grid;
59-
grid-template-columns: 50% 50%;
59+
grid-template-columns: 33.33% 33.33% 33.33%;
6060
place-items:center;
6161
box-sizing: border-box;
6262
border-radius: 10px;

0 commit comments

Comments
 (0)