Add formatting to index.html, handle empty strings in CE and 0 with equals
This commit is contained in:
parent
89252d8a6a
commit
72579e7c76
2 changed files with 12 additions and 4 deletions
|
|
@ -13,18 +13,22 @@
|
|||
<div class="screen">0</div>
|
||||
<button class="clear all">C</button>
|
||||
<button class="clear error">CE</button>
|
||||
|
||||
<button class="num">7</button>
|
||||
<button class="num">8</button>
|
||||
<button class="num">9</button>
|
||||
<button class="operator">*</button>
|
||||
|
||||
<button class="num">4</button>
|
||||
<button class="num">5</button>
|
||||
<button class="num">6</button>
|
||||
<button class="operator">/</button>
|
||||
|
||||
<button class="num">1</button>
|
||||
<button class="num">2</button>
|
||||
<button class="num">3</button>
|
||||
<button class="operator">-</button>
|
||||
|
||||
<button class="num">0</button>
|
||||
<button class="num">.</button>
|
||||
<button class="equal">=</button>
|
||||
|
|
|
|||
12
script.js
12
script.js
|
|
@ -19,8 +19,12 @@ function displayToScreen() {
|
|||
screen.textContent = screen.textContent.slice(0, -1);
|
||||
|
||||
if (!isNaN(screen.textContent.charAt(-2))) {
|
||||
screen.textContent = screen.textContent.trimEnd()
|
||||
}
|
||||
screen.textContent = screen.textContent.trimEnd()
|
||||
}
|
||||
|
||||
if (screen.textContent === "") {
|
||||
screen.textContent = "0"
|
||||
}
|
||||
} else {
|
||||
if (this.className == "operator") {
|
||||
let screenContent = screen.textContent;
|
||||
|
|
@ -43,7 +47,7 @@ function displayToScreen() {
|
|||
|
||||
} else if (screen.textContent === "0" && this.className == "num") {
|
||||
screen.textContent = this.textContent;
|
||||
} else {
|
||||
} else if (this.className == "num") {
|
||||
screen.textContent += this.textContent;
|
||||
}
|
||||
}
|
||||
|
|
@ -106,7 +110,7 @@ function Calculator(str) {
|
|||
b = +parts[2];
|
||||
|
||||
|
||||
if (a && !b) {
|
||||
if ((a || a == "0") && !b) {
|
||||
return a;
|
||||
}
|
||||
if (isNaN(a) || isNaN(b)) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue