const calculator = document.getElementById('calculator');
const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const operations = ['=', '+', '-', '*', '/'];
const generateHTML = () => {
const digits = nums.map(n => `<button class="number" value="${n}">${n}</button>`)
const ops = operations.map(op => `<button class="action" value="${op}">${op}</button>`)
const html = digits.concat(ops)
html.push('<input type="text" value="0" id="output">');
calculator.innerHTML = html.join('')
};
let tempValue, newValue, action
const calc = (value) => {
const output = document.getElementById('output')
switch (value) {
case '+':
action = '+'
tempValue = newValue
newValue = 0
output.value = '0'
console.log(tempValue + '_' + action + '_' + newValue)
break
case '-':
action = '-'
tempValue = newValue
newValue = 0
output.value = '0'
console.log(tempValue + '_' + action + '_' + newValue)
break
case '*':
action = '*'
tempValue = newValue
newValue = 0
output.value = '0'
console.log(tempValue + '_' + action + '_' + newValue)
break
case '/':
action = '/'
tempValue = newValue
newValue = 0
output.value = '0'
console.log(tempValue + '_' + action + '_' + newValue)
break
case '=':
tempValue = parseFloat(tempValue)
newValue = parseFloat(newValue)
console.log(tempValue + '_' + action + '_' + newValue)
switch (action) {
case '+':
output.value = tempValue + newValue
break
case '-':
output.value = tempValue - newValue
break
case '*':
output.value = tempValue * newValue
break
case '/':
output.value = tempValue / newValue
break
}
break
default:
output.value += value
newValue = output.value
console.log(tempValue + '_' + action + '_' + newValue)
}
}
const display = () => {
document.querySelectorAll('button').forEach(button => {
button.addEventListener('click', function(event) {
calc(event.target.value)
})
});
}
const init = () => {
generateHTML();
display();
}
init();
let calc: Calculator = new Calculator(0); //значение currentValue по умолчанию
let result: number = calc
.Add(1) //0+1 = 1
.Substract(5) //1 - -5 = -4
.Multiply(4) //-4 * 4 = -16
.Result; //-16
alert(result);