<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Un calculator simplu</title>
</head>
<body>
<h1>Calculator</h1>
<input type="number" placeholder="a" id="numberA" class="numbers op">
<input type="number" placeholder="b" id="numberB" class="numbers op">
<button onclick="sub()">-</button>
<button onclick="add()">+</button>
<button onclick="mul()">*</button>
<button onclick="div()">/</button>
<button onclick="pow()">^</button>
<button onclick="sqrt()">sqrt</button>
<code id="result"><span>Result</span></code>
<ul id="history"></ul>
<script src="index.js"></script>
</body>
</html>
const numberA = document.querySelector('#numberA');
const numberB = document.querySelector('#numberB');
const result = document.querySelector('#result');
const history = document.querySelector('#history');
function round(r) {
return Math.round(r * 1e10) / 1e10;
}
function sub() {
const a = +numberA.value;
const b = +numberB.value;
result.innerText = round(a - b);
history.innerHTML += '<li>1 + 2 = 4</li>';
}
function add() {
const a = +numberA.value;
const b = +numberB.value;
result.innerText = round(a + b);
}
function mul() {
const a = +numberA.value;
const b = +numberB.value;
result.innerText = round(a * b);
}
function div() {
const a = +numberA.value;
const b = +numberB.value;
result.innerText = round(a / b);
}
function pow() {
const a = +numberA.value;
const b = +numberB.value;
result.innerText = round(a ** b);
}
function sqrt() {
const a = +numberA.value;
const b = +numberB.value;
result.innerText = round(Math.sqrt(a, b));
}