Kalkulator w JavaScript
Zajawka: Kalkulator w JavaScript
Stworzenie kalkulatora w JavaScript to doskonały sposób na naukę interaktywnych aplikacji webowych! Ten projekt pozwala połączyć wiedzę z zakresu HTML, CSS i JavaScript, tworząc w pełni funkcjonalne narzędzie do wykonywania obliczeń matematycznych.
Kalkulator oferuje:
- Podstawowe operacje arytmetyczne (dodawanie, odejmowanie, mnożenie, dzielenie).
- Zaawansowane funkcje, takie jak potęgowanie i pierwiastkowanie.
- Responsywny i nowoczesny interfejs, dostosowany do urządzeń mobilnych.
- Wygodny mechanizm obsługi błędów, dzięki któremu użytkownik zawsze otrzymuje poprawne wyniki.
Idealny zarówno do codziennego użytku, jak i jako projekt startowy dla początkujących programistów. Rozwijaj go, dodając nowe funkcje, takie jak logarytmy czy trygonometrię, aby doskonalić swoje umiejętności programistyczne!
Poniżej kod kalkulatora w #JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Professional Calculator</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="calculator"> <div class="display"> <input type="text" id="result" readonly> </div> <div class="buttons"> <button onclick="clearDisplay()">C</button> <button onclick="deleteLast()">⌫</button> <button onclick="appendOperator('%')">%</button> <button onclick="appendOperator('/')">÷</button> <button onclick="appendNumber('7')">7</button> <button onclick="appendNumber('8')">8</button> <button onclick="appendNumber('9')">9</button> <button onclick="appendOperator('*')">×</button> <button onclick="appendNumber('4')">4</button> <button onclick="appendNumber('5')">5</button> <button onclick="appendNumber('6')">6</button> <button onclick="appendOperator('-')">−</button> <button onclick="appendNumber('1')">1</button> <button onclick="appendNumber('2')">2</button> <button onclick="appendNumber('3')">3</button> <button onclick="appendOperator('+')">+</button> <button onclick="appendNumber('0')" class="zero">0</button> <button onclick="appendNumber('.')">.</button> <button onclick="calculate()">=</button> <button onclick="calculateSquareRoot()">√</button> <button onclick="appendOperator('^')">xʸ</button> </div> </div> <script src="script.js"></script> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; background-color: #f4f4f4; } .calculator { width: 300px; background: #fff; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2); border-radius: 10px; overflow: hidden; } .display { background: #333; color: #fff; text-align: right; padding: 20px; font-size: 2em; } .display input { width: 100%; border: none; background: none; color: inherit; font-size: inherit; text-align: right; } .buttons { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px; background: #ddd; } .buttons button { background: #f4f4f4; border: none; font-size: 1.5em; padding: 20px; cursor: pointer; transition: background 0.3s; } .buttons button:hover { background: #e0e0e0; } .buttons button:active { background: #bbb; } .zero { grid-column: span 2; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | let resultElement = document.getElementById("result"); // Clear the display function clearDisplay() { resultElement.value = ""; } // Delete the last character function deleteLast() { resultElement.value = resultElement.value.slice(0, -1); } // Append a number or operator to the display function appendNumber(number) { resultElement.value += number; } function appendOperator(operator) { const lastChar = resultElement.value.slice(-1); if ("+-*/%^".includes(lastChar)) { resultElement.value = resultElement.value.slice(0, -1) + operator; } else { resultElement.value += operator; } } // Calculate the result function calculate() { try { let expression = resultElement.value.replace("÷", "/").replace("×", "*").replace("^", "**"); resultElement.value = eval(expression); } catch { resultElement.value = "Error"; } } // Calculate square root function calculateSquareRoot() { try { let value = parseFloat(resultElement.value); if (value >= 0) { resultElement.value = Math.sqrt(value); } else { resultElement.value = "Error"; } } catch { resultElement.value = "Error"; } } |


