Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Калькулятор</title>
<link rel="stylesheet" href="style.css">
</head>
<body>

<div class="calculator">
<div class="row">
<div class="input-wrapper">
<input type="text" id="num1" autocomplete="off">
<span class="error-msg" id="err1"></span>
</div>

<div class="select-wrapper">
<select id="op">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
</div>

<div class="input-wrapper">
<input type="text" id="num2" autocomplete="off">
<span class="error-msg" id="err2"></span>
</div>
</div>

<button id="calcBtn">подсчитать</button>

<div class="results" id="results"></div>
</div>

<script src="script.js"></script>
</body>
</html>
72 changes: 72 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
document.addEventListener('DOMContentLoaded', () => {
const qs = selector => document.querySelector(selector);

const num1 = qs('#num1');
const num2 = qs('#num2');
const op = qs('#op');
const calcBtn = qs('#calcBtn');
const resultsDiv = qs('#results');
const err1 = qs('#err1');
const err2 = qs('#err2');

const history = [];

const validate = (input, errEl) => {
const val = input.value.trim().replace(',', '.');

if (val === '') {
input.classList.remove('invalid');
errEl.textContent = '';
return true;
}

if (isNaN(Number(val))) {
input.classList.add('invalid');
errEl.textContent = 'Только числа';
return false;
}

input.classList.remove('invalid');
errEl.textContent = '';
return true;
};

num1.addEventListener('input', () => validate(num1, err1));
num2.addEventListener('input', () => validate(num2, err2));

calcBtn.addEventListener('click', () => {
const isV1Valid = validate(num1, err1);
const isV2Valid = validate(num2, err2);

if (num1.value.trim() === '') { err1.textContent = 'Заполните поле'; num1.classList.add('invalid'); }
if (num2.value.trim() === '') { err2.textContent = 'Заполните поле'; num2.classList.add('invalid'); }

if (!isV1Valid || !isV2Valid || num1.value.trim() === '' || num2.value.trim() === '') return;

const n1 = parseFloat(num1.value.replace(',', '.'));
const n2 = parseFloat(num2.value.replace(',', '.'));
const operator = op.value;
let res;

if (operator === '+') res = n1 + n2;
else if (operator === '-') res = n1 - n2;
else if (operator === '*') res = n1 * n2;
else if (operator === '/') {
if (n2 === 0) {
num2.classList.add('invalid');
err2.textContent = 'Деление на ноль';
return;
}
res = n1 / n2;
}

res = parseFloat(res.toFixed(5));
history.push(`${n1} ${operator} ${n2} = ${res}`);

if (history.length > 5) history.shift();

resultsDiv.innerHTML = history.map((item, index) =>
`<div class="${index === history.length - 1 ? 'current-result' : 'history-item'}">${item}</div>`
).join('');
});
});
125 changes: 125 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
:root {
--bg-color: #a0e08b;
--red: #ff0000;
--pale-red: #d88e8e;
}

body {
background-color: var(--bg-color);
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
padding-top: 50px;
margin: 0;
}

.calculator {
display: flex;
flex-direction: column;
align-items: center;
gap: 30px;
width: 100%;
max-width: 700px;
padding: 0 20px;
}

.row {
display: flex;
justify-content: space-between;
align-items: flex-start;
width: 100%;
gap: 15px;
}

.input-wrapper {
position: relative;
flex: 1;
}

input, select, button, .results {
border: 2px dashed var(--red);
border-radius: 12px;
background: transparent;
font-size: 24px;
color: var(--red);
outline: none;
box-sizing: border-box;
}

input {
width: 100%;
padding: 8px 15px;
height: 45px;
}

input.invalid {
border-style: solid;
background-color: rgba(255, 0, 0, 0.05);
}

.error-msg {
position: absolute;
bottom: -20px;
left: 5px;
font-size: 14px;
color: var(--red);
white-space: nowrap;
}

.select-wrapper {
position: relative;
height: 45px;
}

select {
height: 100%;
padding: 0 35px 0 15px;
cursor: pointer;
appearance: none;
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='red' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E") no-repeat right 10px center;
}

button {
padding: 10px 40px;
cursor: pointer;
transition: transform 0.1s;
}

button:active {
transform: scale(0.97);
}

.results {
width: 100%;
min-height: 100px;
display: flex;
flex-direction: column;
justify-content: flex-end;
padding: 20px;
text-align: left;
}

.history-item {
color: var(--pale-red);
margin-bottom: 8px;
font-size: 22px;
}

.current-result {
font-weight: bold;
color: var(--red);
font-size: 24px;
}

@media (max-width: 550px) {
.row {
flex-direction: column;
gap: 25px;
}
.select-wrapper {
width: 100%;
}
select {
width: 100%;
}
}