Skip to content

Commit 9dbedfc

Browse files
committed
update 07 add buttons ["1", "+", "=", "-"]
1 parent 00c4b55 commit 9dbedfc

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

bjs/07_Number_and_string/index.html

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,28 @@
1515
<div class="form-group">
1616
<div class="card" id="calc">
1717
<div class="card-body">
18-
<input class="form-control input-window" id="inputWindow" value="">
18+
<input class="form-control input-window" id="inputWindow" value="0">
1919
<div class="row no-gutters">
2020
<div class="col">
2121
<button class="btn btn-outline-danger form-control" id="btn_clr">C</button>
2222
</div>
2323
</div>
24+
<div class="row no-gutters">
25+
<div class="col">
26+
<button class="btn btn-outline-primary form-control btn_num" id="btn_1">1</button>
27+
</div>
28+
</div>
29+
<div class="row no-gutters">
30+
<div class="col">
31+
<button class="btn btn-outline-primary form-control btn_op" id="btn_sum">+</button>
32+
</div>
33+
<div class="col">
34+
<button class="btn btn-outline-primary form-control btn_op" id="btn_sub">-</button>
35+
</div>
36+
<div class="col">
37+
<button class="btn btn-outline-primary form-control" id="btn_calc">=</button>
38+
</div>
39+
</div>
2440
</div>
2541
</div>
2642
</div>

bjs/07_Number_and_string/script.js

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,64 @@
11
let lastOperand = 0;
2+
let repeatOperand = 0;
23
let operation = null;
4+
let lastIsOp = false;
35

4-
const inputWindow = document.getElementById('inputWindow');
6+
const inputWindow = document.querySelector("#inputWindow");
57

8+
document.querySelectorAll(".btn_num").forEach(element => {
9+
element.addEventListener("click", function () {
10+
if (lastIsOp)
11+
{
12+
inputWindow.value = "";
13+
lastIsOp = false;
14+
}
15+
if (inputWindow.value == 0) inputWindow.value = "";
16+
inputWindow.value += this.textContent;
17+
});
18+
});
619

7-
document.getElementById('btn_clr').addEventListener('click', function () {
20+
document.querySelectorAll(".btn_op").forEach(element => {
21+
element.addEventListener("click", function () {
22+
repeatOperand = 0;
23+
});
24+
});
25+
26+
document.querySelector("#btn_sum").addEventListener("click", function (){
27+
operation = "sum";
28+
if (!lastIsOp)
29+
{
30+
inputWindow.value = parseInt(inputWindow.value) + lastOperand;
31+
lastOperand = parseInt(inputWindow.value);
32+
}
33+
lastIsOp = true;
34+
});
35+
36+
document.querySelector("#btn_sub").addEventListener("click", function (){
37+
operation = "sub";
38+
if (!lastIsOp)
39+
{
40+
inputWindow.value = parseInt(inputWindow.value) - lastOperand;
41+
lastOperand = parseInt(inputWindow.value);
42+
}
43+
lastIsOp = true;
44+
});
45+
46+
document.querySelector("#btn_calc").addEventListener("click", function (){
47+
if (!lastIsOp)
48+
{
49+
repeatOperand = parseInt(inputWindow.value);
50+
}
51+
if (operation === "sum") inputWindow.value = lastOperand + repeatOperand;
52+
else if (operation === "sub") inputWindow.value = lastOperand - repeatOperand;
53+
lastOperand = parseInt(inputWindow.value);
54+
55+
lastIsOp = true;
56+
});
57+
58+
document.querySelector("#btn_clr").addEventListener("click", function () {
859
lastOperand = 0;
60+
repeatOperand = 0;
961
operation = null;
10-
inputWindow.value = '';
11-
})
12-
62+
lastIsOp = false;
63+
inputWindow.value = "0";
64+
});

0 commit comments

Comments
 (0)