Sure! Below is a complete HTML script that allows a user to input a number labeled "Gunman". Upon submission, it calculates and displays:
- Craftsman: Gunman divided by 4
- Missile Site: Gunman divided by 6
How It Works:
- HTML Structure: Contains a form with an input field for "Gunman" and a button to perform the calculation.
- JavaScript Function: Retrieves the input value, performs the division operations, and displays the results.
- Basic Styling: Added some CSS for better presentation.
Complete HTML Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Gunman Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
padding: 20px;
}
.calculator {
background-color: #fff;
padding: 20px;
border-radius: 5px;
max-width: 400px;
margin: auto;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.calculator h2 {
text-align: center;
}
.calculator label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
.calculator input[type="number"] {
width: 100%;
padding: 8px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
}
.calculator button {
width: 100%;
padding: 10px;
background-color: #4285F4;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
}
.calculator button:hover {
background-color: #357ae8;
}
.results {
margin-top: 20px;
padding: 15px;
background-color: #e7f3fe;
border-left: 6px solid #2196F3;
border-radius: 4px;
}
.error {
color: red;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="calculator">
<h2>Gunman Calculator</h2>
<label for="gunman">Enter Gunman Number:</label>
<input type="number" id="gunman" name="gunman" placeholder="Enter a number" min="0">
<button onclick="calculate()">Calculate</button>
<div id="results" class="results" style="display:none;">
<p><strong>Craftsman:</strong> <span id="craftsman"></span></p>
<p><strong>Missile Site:</strong> <span id="missilesite"></span></p>
</div>
<div id="error" class="error"></div>
</div>
<script>
function calculate() {
// Clear previous results and errors
document.getElementById('results').style.display = 'none';
document.getElementById('error').innerText = '';
// Get the input value
const gunmanInput = document.getElementById('gunman').value;
// Validate the input
if (gunmanInput === '') {
document.getElementById('error').innerText = 'Please enter a number for Gunman.';
return;
}
const gunman = Number(gunmanInput);
if (isNaN(gunman) || gunman < 0) {
document.getElementById('error').innerText = 'Please enter a valid non-negative number.';
return;
}
// Perform calculations
const craftsman = gunman / 4;
const missileSite = gunman / 6;
// Display the results, rounded to two decimal places
document.getElementById('craftsman').innerText = craftsman.toFixed(2);
document.getElementById('missilesite').innerText = missileSite.toFixed(2);
document.getElementById('results').style.display = 'block';
}
</script>
</body>
</html>
How to Use:
- Open the HTML File: Save the above code in a file with a
.html extension, for example, gunman_calculator.html.
- Run in Browser: Open the file using any modern web browser (e.g., Chrome, Firefox, Edge).
- Input Number: Enter a numerical value in the "Gunman" input field.
- Calculate: Click the "Calculate" button to see the results for "Craftsman" and "Missile Site".
Features:
- Input Validation: Ensures the user enters a non-negative number.
- Responsive Design: The calculator is styled to be clean and user-friendly.
- Error Messages: Provides feedback if the input is invalid or missing.
- Rounded Results: Displays results rounded to two decimal places for clarity.
Feel free to customize the styling or functionality as needed!