Write a JavaScript to design a simple calculator to perform the following operations: sum, product, difference and quotient
<!DOCTYPE html>
<html>
<head>
<title>Simple Cal
culator</title>
<script>
// Function to perform the addition operation
function add() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 + num2;
document.getElementById("result").value = result;
}
// Function to perform the subtraction operation
function subtract() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 - num2;
document.getElementById("result").value = result;
}
// Function to perform the multiplication operation
function multiply() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 * num2;
document.getElementById("result").value = result;
}
// Function to perform the division operation
function divide() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 / num2;
if (isNaN(result)) {
document.getElementById("result").value = "Cannot divide by zero";
} else {
document.getElementById("result").value = result;
}
}
</script>
</head>
<body>
<h1>Simple Calculator</h1>
<label for="num1">Number 1:</label>
<input type="number" id="num1"><br>
<label for="num2">Number 2:</label>
<input type="number" id="num2"><br>
<button onclick="add()">Add</button>
<button onclick="subtract()">Subtract</button>
<button onclick="multiply()">Multiply</button>
<button onclick="divide()">Divide</button>
<label for="result">Result:</label>
<input type="text" id="result" readonly>
</body>
</html>
With CSS
<!DOCTYPE html>
<html>
<head>
<title>Country Capitals</title>
<style>
#capital {
color: blue;
font-weight: bold;
font-size: 20px;
}
</style>
</head>
<body>
<h1>Select a Country</h1>
<select id="countrySelect" onchange="showCapital()">
<option value="">-- Select a Country --</option>
<option value="USA">United States</option>
<option value="INDIA">BHARATHA</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Australia">Australia</option>
</select>
<p>Capital: <span id="capital"></span></p>
<script>
function showCapital() {
// Get the selected country from the select element
var selectElement = document.getElementById("countrySelect");
var selectedCountry = selectElement.options[selectElement.selectedIndex].value;
// Set the capital based on the selected country
var capitalElement = document.getElementById("capital");
switch (selectedCountry) {
case "USA":
capitalElement.innerText = "Washington, D.C.";
break;
case "INDIA":
capitalElement.innerText = "Newdelhi";
break;
case "France":
capitalElement.innerText = "Paris";
break;
case "Germany":
capitalElement.innerText = "Berlin";
break;
case "Australia":
capitalElement.innerText = "Canberra";
break;
default:
capitalElement.innerText = "";
break;
}
}
</script>
</body>
</html>
Save the code in an HTML file, and you can open it in a web browser to see your biodata webpage. Remember to include your photo file (e.g., your_photo.jpg) in the same directory as the HTML file or provide the correct file path in the src attribute of the img tag.