ADDITION, SUBTRACT, MULTIPLIES, DIVISION
Simple Calculator
This code implements a simple calculator using HTML and JavaScript. It features a user interface with buttons for digits, arithmetic operations, and a display field that shows the current input and result. The calculator performs basic arithmetic operations: addition, subtraction, multiplication, and division. Each operation is handled by dedicated functions, ensuring clear and maintainable code.
HTML Structure:
- Calculator Form: The calculator encapsulates the entire calculator interface. It contains a display field (input id="display" type="text") to show inputs and calculation results.
- Number and Operator Buttons: The buttons are arranged in a grid to mimic a physical calculator. They include:
JavaScript Functions:
- concatener(chiffre) Function: This function adds the clicked digits or operators to the string displayed on the screen.
- Calculation Functions (addition, subtraction, multiplication, division): These functions perform basic mathematical operations:
addition()
: Adds two numbers.soustraction()
: Subtracts the second number from the first.multiplication()
: Multiplies two numbers.division()
: Divides the first number by the second, with a check to prevent division by zero.- calculer() Function: Responsible for evaluating the complete expression:
- Retrieves the displayed expression as a string.
- Splits the expression into a list of numbers and operators using the split() method.
- Iterates through the list, applies each operator in sequence, and updates the result.
- Displays the final result in the display field.