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
43 changes: 43 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Калькулятор</title>
<link rel="stylesheet" href="style.css">
</head>
<body>

<div class="wrapper">
<div class="calculator">

<div class="inputs-row">

<div class="field-group">
<input type="text" id="num1" placeholder="Введите число">
<div class="error" id="error1"></div>
</div>

<select id="operationSelect" class="operation-select">
<option value="+">+</option>
<option value="-">−</option>
<option value="*">*</option>
<option value="/">/</option>
</select>

<div class="field-group">
<input type="text" id="num2" placeholder="Введите число">
<div class="error" id="error2"></div>
</div>

</div>

<button id="calculateBtn" class="calculate-btn">подсчитать</button>

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

</div>
</div>

<script src="script.js"></script>
</body>
</html>
77 changes: 77 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const num1 = document.getElementById("num1");
const num2 = document.getElementById("num2");
const operationSelect = document.getElementById("operationSelect");
const calculateBtn = document.getElementById("calculateBtn");
const resultsDiv = document.getElementById("results");

const error1 = document.getElementById("error1");
const error2 = document.getElementById("error2");

function validateInput(input, errorElement) {
const value = input.value.trim();

if (value === "") {
errorElement.textContent = "Поле не должно быть пустым";
input.classList.add("error-input");
return false;
}

if (!/^[-+]?\d+(\.\d+)?$/.test(value)) {
errorElement.textContent = "Введите корректное число";
input.classList.add("error-input");
return false;
}

errorElement.textContent = "";
input.classList.remove("error-input");
return true;
}

function calculate() {
const valid1 = validateInput(num1, error1);
const valid2 = validateInput(num2, error2);

if (!valid1 || !valid2) return;

const a = parseFloat(num1.value);
const b = parseFloat(num2.value);
const op = operationSelect.value;

if (op === "/" && b === 0) {
error2.textContent = "Деление на ноль невозможно";
num2.classList.add("error-input");
return;
}

let result;

switch (op) {
case "+": result = a + b; break;
case "-": result = a - b; break;
case "*": result = a * b; break;
case "/": result = a / b; break;
}

addToHistory(`${a} ${op} ${b} = ${result}`);
}

function addToHistory(text) {
const oldResults = resultsDiv.querySelectorAll(".result-new");

oldResults.forEach(el => {
el.classList.remove("result-new");
el.classList.add("result-old");
});

const div = document.createElement("div");
div.classList.add("result-new");
div.textContent = text;

resultsDiv.appendChild(div);

while (resultsDiv.children.length > 10) {
resultsDiv.removeChild(resultsDiv.firstChild);
}
}

calculateBtn.addEventListener("click", calculate);
121 changes: 121 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
* {
box-sizing: border-box;
}

html, body {
height: 100%;
}

body {
margin: 0;
background: white;
font-family: Arial, sans-serif;
border: 60px solid #d3d3d3;
padding: 40px;
}

.wrapper {
min-height: calc(100vh - 120px - 80px);
display: flex;
justify-content: flex-start;
align-items: flex-start;
}

.calculator {
background: #a8e6a3;
padding: 30px;
border-radius: 12px;
width: 100%;
max-width: 650px;
}

.inputs-row {
display: flex;
flex-direction: column;
gap: 20px;
}

.field-group {
flex: 1;
display: flex;
flex-direction: column;
}

input {
width: 100%;
padding: 12px;
font-size: 18px;
border-radius: 12px;
border: 2px dashed red;
background: transparent;
color: red;
outline: none;
}

input.error-input {
background: #ffd6d6;
}

.operation-select {
align-self: center;
padding: 10px 15px;
font-size: 18px;
border-radius: 12px;
border: 2px dashed red;
background: transparent;
color: red;
cursor: pointer;
width: 100px;
}

.error {
color: red;
font-size: 14px;
min-height: 18px;
margin-top: 4px;
}

.calculate-btn {
margin: 5px auto 5px auto;
display: block;
width: 200px;
padding: 12px;
font-size: 18px;
border-radius: 12px;
border: 2px dashed red;
background: transparent;
color: red;
cursor: pointer;
}

.results {
margin-top: 25px;
border-radius: 12px;
border: 2px dashed red;
padding: 15px;
min-height: 100px;
color: red;
display: flex;
flex-direction: column;
gap: 8px;
overflow: hidden;
}

.result-old {
opacity: 0.4;
}

.result-new {
font-weight: bold;
}

@media (min-width: 768px) {
.inputs-row {
flex-direction: row;
align-items: flex-start;
}

.operation-select {
align-self: flex-start;
}
}