🔧 AI Nachrichten The Next Terrorist Attack Is Predictable(10.09.2026 um 23:41 Uhr)
🔧 AI Nachrichten Could A.I. Really Kill All Humans?(10.09.2026 um 23:53 Uhr)
🔧 AI Nachrichten Amazon Prime Video Uses A.I. for Lip-Synced Translations(11.09.2026 um 01:56 Uhr)
🔧 AI Nachrichten McClatchy Makes Deep Job Cuts to Newspapers Around the Country(11.09.2026 um 04:25 Uhr)
🔧 AI Nachrichten Law schools tell students to put AI away(07.09.2026 um 16:37 Uhr)
🔧 AI Nachrichten Can Huawei build China’s answer to ASML?(08.09.2026 um 04:57 Uhr)
🔧 AI Nachrichten AI is ushering in an era of mass toe-treading at work(08.09.2026 um 06:00 Uhr)
🔧 AI Nachrichten The Next Terrorist Attack Is Predictable(10.09.2026 um 23:41 Uhr)
🔧 AI Nachrichten Could A.I. Really Kill All Humans?(10.09.2026 um 23:53 Uhr)
🔧 AI Nachrichten Amazon Prime Video Uses A.I. for Lip-Synced Translations(11.09.2026 um 01:56 Uhr)
🔧 AI Nachrichten McClatchy Makes Deep Job Cuts to Newspapers Around the Country(11.09.2026 um 04:25 Uhr)
🔧 AI Nachrichten Law schools tell students to put AI away(07.09.2026 um 16:37 Uhr)
🔧 AI Nachrichten Can Huawei build China’s answer to ASML?(08.09.2026 um 04:57 Uhr)
🔧 AI Nachrichten AI is ushering in an era of mass toe-treading at work(08.09.2026 um 06:00 Uhr)

🔧 Programmierung 🕛 vor 2 Jahren 8 Min Lesezeit
0

Building a Calculator with HTML, CSS, and JavaScript

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

In this article, we will discuss how to create a calculator using HTML, CSS, and JavaScript. This project focuses on the most fundamental functionalities, making it an excellent practice project to improve your programming skills as a beginner.



➡️






Creating the calculator layout



As usual, let's start by creating the HTML skeleton.




CODE
<div class="calculator">
<div class="display">0</div>
<div class="buttons">
<div class="button function">AC</div>
<div class="button function">+/-</div>
<div class="button function">%</div>
<div class="button function">/</div>

<div class="button number">7</div>
<div class="button number">8</div>
<div class="button number">9</div>
<div class="button function">*</div>

<div class="button number">4</div>
<div class="button number">5</div>
<div class="button number">6</div>
<div class="button function">-</div>

<div class="button number">1</div>
<div class="button number">2</div>
<div class="button number">3</div>
<div class="button function">+</div>

<div class="button number">0</div>
<div class="button function">.</div>
<div class="button function">=</div>
</div>
</div>








  • .calculator is a container that wraps around the whole calculator.


  • .display shows the user input, as well as the output of the calculation.


  • .buttons contains both the .number buttons (0-9) and the .function buttons (+, -, *...).



And, of course, you must set up the event listeners so that when a button is clicked, the corresponding function is executed.




CODE
<div class="calculator">
<div class="display" id="display">0</div>
<div class="buttons">
<div class="button function" onclick="clearDisplay()">AC</div>
<div class="button function" onclick="toggleSign()">+/-</div>
<div class="button function" onclick="percent()">%</div>
<div class="button function" onclick="appendOperator('/')">/</div>

<div class="button number" onclick="appendNumber('7')">7</div>
<div class="button number" onclick="appendNumber('8')">8</div>
<div class="button number" onclick="appendNumber('9')">9</div>
<div class="button function" onclick="appendOperator('*')">*</div>

<div class="button number" onclick="appendNumber('4')">4</div>
<div class="button number" onclick="appendNumber('5')">5</div>
<div class="button number" onclick="appendNumber('6')">6</div>
<div class="button function" onclick="appendOperator('-')">-</div>

<div class="button number" onclick="appendNumber('1')">1</div>
<div class="button number" onclick="appendNumber('2')">2</div>
<div class="button number" onclick="appendNumber('3')">3</div>
<div class="button function" onclick="appendOperator('+')">+</div>

<div class="button number" id="Num0" onclick="appendNumber('0')">0</div>
<div class="button function" onclick="appendOperator('.')">.</div>
<div class="button function" onclick="calculate()">=</div>
</div>
</div>






Before we start coding, we also need to discuss the general structure of this project.



First of all, there should be a global variable (displayValue) to store the displayed value, as well as a helper function (updateDisplay()) that updates the .display based on the variable. The function selects the display element and updates its content.



There should also be an appendNumber() and appendOperator() functions that append new digits to displayValue. Lastly, you need a calculate() function that executes the mathematical expression and returns the value.



And of course, there are other functions such as clearDisplay(), toggleSign(), and percent().






Adding styles



Next, you should add some styles so that our calculator actually looks like a calculator.



First of all, the .buttons should be a



The number 0 is a special case, as it .







clearDisplay()






CODE
function clearDisplay() {
displayValue = "";
updateDisplay();
}






Lastly, the clearDisplay() function resets displayValue to an empty string.






Conclusion



You may have noticed we neglected two functions, toggleSign() and percent(). The exact implementation of these functions is available in the .



Happy coding!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
Could A.I. Really Kill All Humans?
1 Quelle
The Next Terrorist Attack Is Predictable
1 Quelle
Anthropic Says It Blocked Possible Efforts to Build Biological Weapons
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Building a Calculator with HTML, CSS, and JavaScript

Thematisch verwandte Begriffe: Building, Calculator, with, HTML · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...