Create a fully functional, dynamic subnet calculator as a single HTML file. The entire…
- User
Create a fully functional, dynamic subnet calculator as a single HTML file. The entire application, including all HTML structure, CSS styling, and JavaScript logic, must be contained within this one file.
Project Requirements:
- Single-File Architecture: Do not use any external CSS or JavaScript files. All styles should be in a
<style>tag and all scripts in a<script>tag within the HTML file. - UI/UX Design:
- The design should be professional, modern, and responsive.
- Use a clean, dark theme.
- The layout should be intuitive, with input fields for an IP address and a CIDR subnet mask (e.g., /24).
- Functionality:
- Calculations must happen in real-time as the user types, without needing to press a "submit" button.
- Display the following results dynamically:
- Network Address
- Broadcast Address
- Total Number of Hosts
- Number of Usable Hosts
- Subnet Mask
- Wildcard Mask
- IP Address Class (A, B, C, etc.)
- IP type (Public or Private)
- Technology: Use plain HTML, CSS, and JavaScript. Do not use any external frameworks or libraries (like React, Vue, or jQuery).
- Single-File Architecture: Do not use any external CSS or JavaScript files. All styles should be in a
Assistant
Below is a fully functional, dynamic subnet calculator contained within a single HTML file. This file includes all necessary HTML structure, CSS styling, and JavaScript logic.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Subnet Calculator</title> <style> body { font-family: Arial, sans-serif; background-color: #1e1e1e; color: #ffffff; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } .container { background-color: #333; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); width: 300px; } .input-group { margin-bottom: 15px; } .input-group label { display: block; margin-bottom: 5px; } .input-group input { width: 100%; padding: 8px; border: 1px solid #555; border-radius: 4px; background-color: #444; color: #fff; } .result { margin-top: 20px; } .result div { margin-bottom: 10px; } </style> </head> <body> <div class="container"> <h2>Subnet Calculator</h2> <div class="input-group"> <label for="ipAddress">IP Address:</label> <input type="text" id="ipAddress" placeholder="e.g., 192.168.1.1"> </div> <div class="input-group"> <label for="cidr">CIDR Subnet Mask:</label> <input type="text" id="cidr" placeholder="e.g., /24"> </div> <div class="result"> <div><strong>Network Address:</strong> <span id="networkAddress"></span></div> <div><strong>Broadcast Address:</strong> <span id="broadcastAddress"></span></div> <div><strong>Total Number of Hosts:</strong> <span id="totalHosts"></span></div> <div><strong>Number of Usable Hosts:</strong> <span id="usableHosts"></span></div> <div><strong>Subnet Mask:</strong> <span id="subnetMask"></span></div> <div><strong>Wildcard Mask:</strong> <span id="wildcardMask"></span></div> <div><strong>IP Address Class:</strong> <span id="ipClass"></span></div> <div><strong>IP Type:</strong> <span id="ipType"></span></div> </div> </div> <script> function ipToInt(ip) { return ip.split('.').reduce((acc, octet) => (acc << 8) + parseInt(octet, 10), 0) >>> 0; } function intToIp(int) { return ((int >> 24) & 0xff) + '.' + ((int >> 16) & 0xff) + '.' + ((int >> 8) & 0xff) + '.' + (int & 0xff); } function calculateSubnet() { const ipAddress = document.getElementById('ipAddress').value; const cidr = document.getElementById('cidr').value; if (!ipAddress || !cidr) { document.querySelectorAll('.result span').forEach(span => span.textContent = ''); return; } const ipParts = ipAddress.split('.').map(Number); const cidrParts = cidr.split('/'); const prefixLength = parseInt(cidrParts[1], 10); if (ipParts.length !== 4 || isNaN(prefixLength) || prefixLength < 0 || prefixLength > 32) { document.querySelectorAll('.result span').forEach(span => span.textContent = ''); return; } const ipInt = ipToInt(ipAddress); const mask = (0xffffffff << (32 - prefixLength)) >>> 0; const networkAddress = ipInt & mask; const broadcastAddress = networkAddress | ~mask; const totalHosts = 2 ** (32 - prefixLength); const usableHosts = totalHosts - 2; const subnetMask = intToIp(mask); const wildcardMask = intToIp(~mask); const ipClass = (ipParts[0] >= 1 && ipParts[0] <= 126) ? 'A' : (ipParts[0] >= 128 && ipParts[0] <= 191) ? 'B' : (ipParts[0] >= 192 && ipParts[0] <= 223) ? 'C' : (ipParts[0] >= 224 && ipParts[0] <= 239) ? 'D' : (ipParts[0] >= 240 && ipParts[0] <= 255) ? 'E' : 'Unknown'; const ipType = (ipParts[0] >= 10 || (ipParts[0] === 172 && ipParts[1] >= 16 && ipParts[1] <= 31) || (ipParts[0] === 192 && ipParts[1] === 168)) ? 'Private' : 'Public'; document.getElementById('networkAddress').textContent = intToIp(networkAddress); document.getElementById('broadcastAddress').textContent = intToIp(broadcastAddress); document.getElementById('totalHosts').textContent = totalHosts; document.getElementById('usableHosts').textContent = usableHosts; document.getElementById('subnetMask').textContent = subnetMask; document.getElementById('wildcardMask').textContent = wildcardMask; document.getElementById('ipClass').textContent = ipClass; document.getElementById('ipType').textContent = ipType; } document.getElementById('ipAddress').addEventListener('input', calculateSubnet); document.getElementById('cidr').addEventListener('input', calculateSubnet); </script> </body> </html>Explanation:
-
HTML Structure:
- The HTML structure includes input fields for the IP address and CIDR subnet mask.
- The results are displayed in a
divwith the classresult.
-
CSS Styling:
- The CSS provides a dark theme with a clean, modern look.
- The layout is responsive and centered on the page.
-
JavaScript Logic:
- The JavaScript converts IP addresses to integers and vice versa.
- It calculates the network address, broadcast address, total number of hosts, number of usable hosts, subnet mask, wildcard mask, IP address class, and IP type.
- The calculations are performed in real-time as the user types in the input fields.
This single HTML file contains all the necessary components to create a fully functional, dynamic subnet calculator.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.