Project Name: Simple Calculator
Project Description:
Create a simple calculator that can perform basic arithmetic operations like addition, subtraction, multiplication, and division. The calculator should have a user interface where the user can input two numbers and select the operation they want to perform. Once the user clicks on the "Calculate" button, the calculator should display the result.
Technical Details:
- Create an HTML file that contains the necessary elements for the user interface (two input fields, a dropdown to select the operation, and a "Calculate" button).
- Use JavaScript to handle the user's input and perform the selected operation.
- Display the result of the calculation in a separate HTML element (e.g., a paragraph).
Here's some sample code to get you started:
HTML: index.html
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<label for="num1">Number 1:</label>
<input type="text" id="num1"><br><br>
<label for="num2">Number 2:</label>
<input type="text" id="num2"><br><br>
<label for="operation">Operation:</label>
<select id="operation">
<option value="add">+</option>
<option value="subtract">-</option>
<option value="multiply">*</option>
<option value="divide">/</option>
</select><br><br>
<button onclick="calculate()">Calculate</button><br><br>
<p id="result"></p>
<script src="./calculator.js"></script>
</body>
</html>JavaScript: calculator.js*
function calculate() {
// Get user input
let num1 = parseInt(document.getElementById("num1").value);
let num2 = parseInt(document.getElementById("num2").value);
let operation = document.getElementById("operation").value;
// Perform calculation
let result;
if (operation === "add") {
result = num1 + num2;
} else if (operation === "subtract") {
result = num1 - num2;
} else if (operation === "multiply") {
result = num1 * num2;
} else if (operation === "divide") {
result = num1 / num2;
}
// Display result
document.getElementById("result").innerHTML = "Result: " + result;
}This is just a basic implementation of a simple calculator, but you can build on this project and add more functionality to make it more robust and user-friendly.
