Description:
Your task is to create a basic calculator that’s easy to use and can handle addition, subtraction, multiplication, and division.
1. Design the Calculator:
Design a simple layout using HTML. Include areas for users to input two numbers, buttons for each operation (addition, subtraction, multiplication, and division), and a space to display the result.
2.Write JavaScript Functions:
Develop JavaScript functions to manage user interactions and perform calculations.
– Obtain the numbers entered by the user.
– Determine the operation based on the selected button.
– Carry out the chosen operation.
– Display the result.
3. Test Your Calculator:
Thoroughly test the calculator with different number combinations and operations to ensure its accuracy and functionality.
Calculator code
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<label for="num1">Enter first number:</label>
<input type="number" id="num1"><br>
<label for="num2">Enter second number:</label>
<input type="number" id="num2"><br>
<button onclick="calculate('+')">Add</button>
<button onclick="calculate('-')">Subtract</button>
<button onclick="calculate('*')">Multiply</button>
<button onclick="calculate('/')">Divide</button><br>
<div id="result"></div>
<script>
function calculate(operator) {
const num1 = parseFloat(document.getElementById('num1').value);
const num2 = parseFloat(document.getElementById('num2').value);
let result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
document.getElementById('result').textContent = 'Result: ' + result;
}
</script>
</body>
</html>
function calculate(operator) { ... }
: This line defines a function namedcalculate
that takes one parameter calledoperator
. This function is responsible for performing arithmetic calculations based on the operator selected by the user.const num1 = parseFloat(document.getElementById('num1').value);
: This line retrieves the value entered by the user in the input field with the IDnum1
and converts it to a floating-point number usingparseFloat()
. It stores the converted value in the variablenum1
.const num2 = parseFloat(document.getElementById('num2').value);
: Similar to the previous line, this line retrieves the value entered by the user in the input field with the IDnum2
, converts it to a floating-point number, and stores it in the variablenum2
.let result;
: This line declares a variable namedresult
without assigning it a value. It will be used to store the result of the arithmetic operation.switch (operator) { ... }
: Thisswitch
statement evaluates theoperator
parameter passed to thecalculate
function and executes a corresponding block of code based on its value.- Inside the
switch
statement:case '+': result = num1 + num2; break;
: If theoperator
is'+'
(addition), it addsnum1
andnum2
together and assigns the result to theresult
variable.case '-': result = num1 - num2; break;
: If theoperator
is'-'
(subtraction), it subtractsnum2
fromnum1
and assigns the result to theresult
variable.case '*': result = num1 * num2; break;
: If theoperator
is'*'
(multiplication), it multipliesnum1
bynum2
and assigns the result to theresult
variable.case '/': result = num1 / num2; break;
: If theoperator
is'/'
(division), it dividesnum1
bynum2
and assigns the result to theresult
variable.
document.getElementById('result').textContent = 'Result: ' + result;
: This line updates the content of the element with the IDresult
to display the calculated result. It concatenates the string'Result: '
with the value of theresult
variable and sets it as the text content of the element.
This code creates a simple calculator with HTML input fields for two numbers, buttons for different operations, and a display area for the result. The JavaScript function calculate() is called when a button is clicked, which performs the corresponding mathematical operation based on the operator passed as an argument. The result is then displayed in the designated area.
<!–more–>