write me a normal script and add bootstrap and icons and design and full calculator…
- User
write me a normal script and add bootstrap and icons and design and full calculator things +
15
- Assistant
- User
добавь вычитание умножение и деление
- Assistant
15
добавь вычитание умножение и деление
To create a full-featured calculator using HTML, Bootstrap for styling, and JavaScript for functionality, you can follow the example below. The calculator will handle basic addition, and you can extend it for more operations later if needed. Additionally, Bootstrap and icons from Font Awesome are included for better presentation.
Here’s how you can set it up:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<!-- Bootstrap CSS -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<!-- Font Awesome Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<style>
.calculator {
margin: 50px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
width: 300px;
text-align: center;
}
#result {
font-size: 1.5rem;
margin-top: 15px;
}
</style>
</head>
<body>
<div class="calculator">
<h2>Simple Calculator</h2>
<div class="form-group">
<input id="input1" type="number" class="form-control" value="10" placeholder="Enter number 1">
</div>
<div class="form-group">
<input id="input2" type="number" class="form-control" value="2" placeholder="Enter number 2">
</div>
<div class="form-group">
<button id="button" class="btn btn-primary btn-block">
<i class="fas fa-calculator"></i> Calculate
</button>
</div>
<div id="result" class="alert alert-info" role="alert">
Result: <span id="output">15</span>
</div>
</div>
<!-- Bootstrap and jQuery JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
document.getElementById('button').addEventListener('click', function() {
// Get the input values
const input1 = parseFloat(document.getElementById('input1').value);
const input2 = parseFloat(document.getElementById('input2').value);
// Perform the addition
const result = input1 + input2;
// Display the result
document.getElementById('output').innerText = result;
});
</script>
</body>
</html>
Bootstrap & Font Awesome: The Bootstrap CSS and JS are included via CDN for styling and layout purposes. Font Awesome is included for the calculator icon on the button.
Calculator Structure: The HTML structure contains two input fields for entering numbers, a button to trigger the calculation, and a section to display the result.
CSS Styles: Added minimal custom CSS for proper styling of the calculator, like margins, paddings, and shadow for visual appearance.
JavaScript Functionality: A simple script is added at the end of the body that listens for a button click, retrieves the values from the input fields, performs the addition, and then updates the result on the page.
User Interaction: Users can input their numbers, click the "Calculate" button, and see the result dynamically.
You can extend this calculator with additional operations (subtraction, multiplication, division) by adding more buttons and updating the JavaScript logic accordingly.
Конечно! Давайте обновим калькулятор, чтобы он поддерживал не только сложение, но и вычитание, умножение и деление. Мы добавим выпадающий список для выбора операции и кнопки для выполнения вычислений. Вот обновленный код:
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Калькулятор</title>
<!-- Bootstrap CSS -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<!-- Font Awesome Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<style>
.calculator {
margin: 50px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
text-align: center;
}
#result {
font-size: 1.5rem;
margin-top: 15px;
}
</style>
</head>
<body>
<div class="calculator">
<h2>Простой калькулятор</h2>
<div class="form-group">
<input id="input1" type="number" class="form-control" value="10" placeholder="Введите число 1">
</div>
<div class="form-group">
<input id="input2" type="number" class="form-control" value="2" placeholder="Введите число 2">
</div>
<div class="form-group">
<select id="operation" class="form-control">
<option value="add">Сложить</option>
<option value="subtract">Вычесть</option>
<option value="multiply">Умножить</option>
<option value="divide">Разделить</option>
</select>
</div>
<div class="form-group">
<button id="calculateButton" class="btn btn-primary btn-block">
<i class="fas fa-calculator"></i> Посчитать
</button>
</div>
<div id="result" class="alert alert-info" role="alert">
Результат: <span id="output">15</span>
</div>
</div>
<!-- Bootstrap and jQuery JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
document.getElementById('calculateButton').addEventListener('click', function () {
// Получаем значения из полей ввода
const input1 = parseFloat(document.getElementById('input1').value);
const input2 = parseFloat(document.getElementById('input2').value);
const operation = document.getElementById('operation').value;
let result;
// Выполняем операцию в зависимости от выбора
switch (operation) {
case 'add':
result = input1 + input2;
break;
case 'subtract':
result = input1 - input2;
break;
case 'multiply':
result = input1 * input2;
break;
case 'divide':
result = input2 !== 0 ? input1 / input2 : 'Ошибка: Деление на ноль';
break;
default:
result = 'Неизвестная операция';
}
// Отображаем результат
document.getElementById('output').innerText = result;
});
</script>
</body>
</html>
Выпадающий список: Добавлен элемент <select> для выбора операции (сложение, вычитание, умножение, деление).
Изменение логики JavaScript: В обработчике события теперь осуществляется проверка выбранной операции с помощью switch. В зависимости от выбранной операции, выполняется соответствующее вычисление. Для деления также добавлена проверка на деление на ноль.
Обновленный результат: Результат вычислений отображается в том же месте, как и раньше.
Теперь у вас есть калькулятор, который может выполнять основные арифметические операции!