class Calculator{
constructor (previousOperandTextElement,currentOperandTextELement){
this.previousOperandTextElement = previousOperandTextElement;
this.currentOperandTextELement = currentOperandTextELement;
this.z = false;
this.clear()
}
clear(){
this.currentOperand = '';
this.previousOperand = '';
this.operation = undefined;
}
delete(){
this.currentOperand = this.currentOperand.toString().slice(0,-1);
}
appendNumber(number){
if(number === '.' && this.currentOperand.includes('.')) return
this.currentOperand = this.currentOperand.toString() + number.toString();
}
choiseOperation(operation){
if(this.currentOperand === '') return
this.operation = operation;
this.previousOperand = this.currentOperand;
this.currentOperand = ''
}
getDisplayNumber(number){
const stringNumber = number.toString()
const integerDigits = parseFloat(stringNumber.split('.')[0])
const decimalDigits = stringNumber.split('.')[1];
let integerDisplay
if(isNaN(integerDigits)){
integerDisplay = ''
}else{
integerDisplay = integerDigits.toLocaleString('ru',{
maximumFractionDigits:0
})
if(decimalDigits != null){
return `${integerDisplay}.${decimalDigits}`
}else{
return integerDisplay
}
}
const floatNumber = parseFloat(number)
if(isNaN(floatNumber)) return ""
return floatNumber.toLocaleString('ru')
}
compute(){
let compution;
const prev = parseFloat(this.previousOperand)
const current = parseFloat(this.currentOperand)
if(isNaN(prev) || isNaN(current)) return
switch(this.operation){
case "+" :
compution = prev + current;
break
case "-" :
compution = prev - current;
break
case "*" :
compution = prev * current;
break
case "/" :
compution = prev / current;
break
default:
return
}
this.currentOperand = +compution.toFixed(5);
this.operation = undefined;
this.previousOperand = "";
}
sq(){
const prev = parseFloat(this.currentOperand)
this.currentOperand = Math.sqrt(prev);
}
min(){
this.z = true
// const integerDigits = parseFloat(stringNumber.split('.')[0])
// const prev = parseFloat(this.currentOperand)
}
updateDisplay(){
this.currentOperandTextELement.innerHTML =
`${this.getDisplayNumber(this.currentOperand)}`
if(this.operation != null){
this.previousOperandTextElement.innerHTML =
`${this.getDisplayNumber(this.previousOperand)} ${this.operation}`;
}else{
this.previousOperandTextElement.innerHTML = ""
}
}
}
let number = document.querySelectorAll('.clock-fase__num');
let display = document.querySelector("#display");
let operation = document.querySelectorAll('.clock-fase__operation');
const clearBtns = document.querySelector('.clear-btn');
const decimalBtn = document.querySelector('.decimal');
const sqrt = document.querySelector('.sqrt');
const previousOperandTextElement = document.querySelector('[data-previous-operand]');
const currentOperandTextElement = document.querySelector('[data-current-operand]');
const equalsButton = document.querySelector('.equals__button');
const deleteButton = document.querySelector('.delete__btn');
const minus = document.querySelector('.minus');
const calculator = new Calculator(previousOperandTextElement,currentOperandTextElement);
number.forEach(button => {
button.addEventListener('click',()=>{
calculator.appendNumber(button.innerHTML);
calculator.updateDisplay()
})
})
operation.forEach(button => {
button.addEventListener('click',()=>{
calculator.choiseOperation(button.innerHTML);
calculator.updateDisplay()
})
})
equalsButton.addEventListener('click',()=>{
calculator.compute();
calculator.updateDisplay();
})
clearBtns.addEventListener('click',()=>{
calculator.clear();
calculator.updateDisplay();
})
deleteButton.addEventListener('click',()=>{
calculator.delete();
calculator.updateDisplay();
})
sqrt.addEventListener('click',()=>{
calculator.sq();
calculator.updateDisplay();
})
minus.addEventListener('click',()=>{
calculator.min();
calculator.updateDisplay();
})