From 496a077dc95a0a16cf944958e41fa9828c957e68 Mon Sep 17 00:00:00 2001 From: Zhenya84 <55429535+Zhenya84@users.noreply.github.com> Date: Wed, 14 Oct 2020 17:26:33 +0300 Subject: [PATCH 1/4] Update index.html --- index.html | 63 +++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/index.html b/index.html index df6b231..d55ce1e 100644 --- a/index.html +++ b/index.html @@ -1,37 +1,38 @@ - - - - Calculator - - + + + Calculator + + -
-
-
-
-
- - - - - - - - - - - - - - - - - - -
+
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + - \ No newline at end of file + From bad5b93465ca4eaa3be111b49ba3f6c65f524e68 Mon Sep 17 00:00:00 2001 From: Zhenya84 <55429535+Zhenya84@users.noreply.github.com> Date: Wed, 14 Oct 2020 17:29:12 +0300 Subject: [PATCH 2/4] Update styles.css --- styles.css | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/styles.css b/styles.css index 11b6fdb..880e88b 100644 --- a/styles.css +++ b/styles.css @@ -16,7 +16,7 @@ body { align-content: center; min-height: 100vh; grid-template-columns: repeat(4, 100px); - grid-template-rows: minmax(120px, auto) repeat(5, 100px); + grid-template-rows: minmax(120px, auto) repeat(6, 100px); } .calculator-grid > button { @@ -31,10 +31,6 @@ body { background-color: rgba(255, 255, 255, .9); } -.span-two { - grid-column: span 2; -} - .output { grid-column: 1 / -1; background-color: rgba(0, 0, 0, .75); @@ -55,4 +51,8 @@ body { .output .current-operand { color: white; font-size: 2.5rem; -} \ No newline at end of file +} + +.span-four { + grid-column: span 4; +} From 3177059d87bb24bd816da8bc6198ba6148613d86 Mon Sep 17 00:00:00 2001 From: Zhenya84 <55429535+Zhenya84@users.noreply.github.com> Date: Wed, 14 Oct 2020 17:30:02 +0300 Subject: [PATCH 3/4] Update script.js --- script.js | 80 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 18 deletions(-) diff --git a/script.js b/script.js index f471622..2184e32 100644 --- a/script.js +++ b/script.js @@ -3,14 +3,36 @@ class Calculator { this.previousOperandTextElement = previousOperandTextElement; this.currentOperandTextElement = currentOperandTextElement; this.readyToReset = false; + this.sign = false; this.clear(); } + root() { + debugger; + if (this.currentOperand === '') return; + if (this.currentOperand < 0) return this.currentOperand = 'Ошибка'; + this.currentOperand = Math.sqrt(this.currentOperand); + } + + signChange() { + debugger; + if (this.currentOperand === '' || this.currentOperand == 0) return; + else if (this.sign == false && this.currentOperand > 0) { + this.currentOperand = `-${this.currentOperand}`; + this.sign = true; + } + else { + this.currentOperand = Math.abs(this.currentOperand); + this.sign = false; + } + } + clear() { this.currentOperand = ''; this.previousOperand = ''; this.operation = undefined; this.readyToReset = false; + this.sign = false; } delete() { @@ -24,34 +46,39 @@ class Calculator { chooseOperation(operation) { if (this.currentOperand === '') return; - if (this.previousOperand !== '' && this.previousOperand !== '') { + if (this.currentOperand !== '' && this.previousOperand !== '') { this.compute(); } this.operation = operation; this.previousOperand = this.currentOperand; this.currentOperand = ''; + this.sign = false; } compute() { + debugger; let computation; const prev = parseFloat(this.previousOperand); const current = parseFloat(this.currentOperand); - if (isNaN(prev) || isNaN(current)) return; + if (isNaN(prev) || isNaN(current)) return switch (this.operation) { - case '+': - computation = prev + current; - break - case '-': - computation = prev - current; - break - case '*': - computation = prev * current; - break - case '÷': - computation = prev / current; - break - default: - return; + case '+': + computation = (prev * 10 + current * 10) / 10; + break + case '-': + computation = (prev * 10 - current * 10) / 10; + break + case '*': + computation = ((prev * 10) * (current * 10)) / 100; + break + case '÷': + computation = (prev * 10 / current * 10) / 100; + break + case 'pow': + computation = Math.pow(prev, current); + break + default: + return; } this.readyToReset = true; this.currentOperand = computation; @@ -60,10 +87,11 @@ class Calculator { } getDisplayNumber(number) { + debugger; const stringNumber = number.toString() const integerDigits = parseFloat(stringNumber.split('.')[0]) const decimalDigits = stringNumber.split('.')[1] - let integerDisplay + let integerDisplay; if (isNaN(integerDigits)) { integerDisplay = '' } else { @@ -77,6 +105,7 @@ class Calculator { } updateDisplay() { + if (this.currentOperand === 'Ошибка') return this.currentOperandTextElement.innerText = this.currentOperand; this.currentOperandTextElement.innerText = this.getDisplayNumber(this.currentOperand) if (this.operation != null) { @@ -88,7 +117,6 @@ class Calculator { } } - const numberButtons = document.querySelectorAll('[data-number]'); const operationButtons = document.querySelectorAll('[data-operation]'); const equalsButton = document.querySelector('[data-equals]'); @@ -96,10 +124,13 @@ const deleteButton = document.querySelector('[data-delete]'); const allClearButton = document.querySelector('[data-all-clear]'); const previousOperandTextElement = document.querySelector('[data-previous-operand]'); const currentOperandTextElement = document.querySelector('[data-current-operand]'); +const signChangeButton = document.querySelector('[data-minus-plus]'); +const rootButton = document.querySelector('[data-root]'); const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement) numberButtons.forEach(button => { + debugger; button.addEventListener("click", () => { if(calculator.previousOperand === "" && @@ -114,6 +145,7 @@ numberButtons.forEach(button => { }) operationButtons.forEach(button => { + debugger; button.addEventListener('click', () => { calculator.chooseOperation(button.innerText); calculator.updateDisplay(); @@ -134,3 +166,15 @@ deleteButton.addEventListener('click', button => { calculator.delete(); calculator.updateDisplay(); }) + +signChangeButton.addEventListener('click', button => { + debugger; + calculator.signChange(); + calculator.updateDisplay(); +}) + +rootButton.addEventListener('click', button => { + debugger; + calculator.root(); + calculator.updateDisplay(); +}) From fec3eff9452778ded5e87fe9085892c2e524a7d5 Mon Sep 17 00:00:00 2001 From: Zhenya84 <55429535+Zhenya84@users.noreply.github.com> Date: Thu, 15 Oct 2020 08:45:25 +0300 Subject: [PATCH 4/4] Update script.js --- script.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/script.js b/script.js index 2184e32..b0cad50 100644 --- a/script.js +++ b/script.js @@ -8,14 +8,12 @@ class Calculator { } root() { - debugger; if (this.currentOperand === '') return; if (this.currentOperand < 0) return this.currentOperand = 'Ошибка'; this.currentOperand = Math.sqrt(this.currentOperand); } signChange() { - debugger; if (this.currentOperand === '' || this.currentOperand == 0) return; else if (this.sign == false && this.currentOperand > 0) { this.currentOperand = `-${this.currentOperand}`; @@ -56,7 +54,6 @@ class Calculator { } compute() { - debugger; let computation; const prev = parseFloat(this.previousOperand); const current = parseFloat(this.currentOperand); @@ -87,7 +84,6 @@ class Calculator { } getDisplayNumber(number) { - debugger; const stringNumber = number.toString() const integerDigits = parseFloat(stringNumber.split('.')[0]) const decimalDigits = stringNumber.split('.')[1] @@ -130,7 +126,6 @@ const rootButton = document.querySelector('[data-root]'); const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement) numberButtons.forEach(button => { - debugger; button.addEventListener("click", () => { if(calculator.previousOperand === "" && @@ -145,7 +140,6 @@ numberButtons.forEach(button => { }) operationButtons.forEach(button => { - debugger; button.addEventListener('click', () => { calculator.chooseOperation(button.innerText); calculator.updateDisplay(); @@ -168,13 +162,11 @@ deleteButton.addEventListener('click', button => { }) signChangeButton.addEventListener('click', button => { - debugger; calculator.signChange(); calculator.updateDisplay(); }) rootButton.addEventListener('click', button => { - debugger; calculator.root(); calculator.updateDisplay(); })