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 + diff --git a/script.js b/script.js index f471622..b0cad50 100644 --- a/script.js +++ b/script.js @@ -3,14 +3,34 @@ class Calculator { this.previousOperandTextElement = previousOperandTextElement; this.currentOperandTextElement = currentOperandTextElement; this.readyToReset = false; + this.sign = false; this.clear(); } + root() { + if (this.currentOperand === '') return; + if (this.currentOperand < 0) return this.currentOperand = 'Ошибка'; + this.currentOperand = Math.sqrt(this.currentOperand); + } + + signChange() { + 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 +44,38 @@ 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() { 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; @@ -63,7 +87,7 @@ class Calculator { 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 +101,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 +113,6 @@ class Calculator { } } - const numberButtons = document.querySelectorAll('[data-number]'); const operationButtons = document.querySelectorAll('[data-operation]'); const equalsButton = document.querySelector('[data-equals]'); @@ -96,6 +120,8 @@ 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) @@ -134,3 +160,13 @@ deleteButton.addEventListener('click', button => { calculator.delete(); calculator.updateDisplay(); }) + +signChangeButton.addEventListener('click', button => { + calculator.signChange(); + calculator.updateDisplay(); +}) + +rootButton.addEventListener('click', button => { + calculator.root(); + calculator.updateDisplay(); +}) 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; +}