Spaces:
Running
Running
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Dynamic Average Calculator</title> | |
| <style> | |
| body { | |
| font-family: sans-serif, Arial, Helvetica; | |
| background-color: #ffffff; | |
| display: flex; | |
| justify-content: center; | |
| align-items: flex-start; /* Align to top to handle growing content */ | |
| min-height: 100vh; | |
| margin: 0; | |
| padding: 30px; | |
| box-sizing: border-box; | |
| } | |
| .container { | |
| background-color: #f9f9f9; | |
| padding: 30px; | |
| border-radius: 15px; | |
| box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); | |
| width: 100%; | |
| max-width: 450px; | |
| text-align: center; | |
| border-top: 5px solid #8A2BE2; | |
| } | |
| h1 { | |
| color: #8A2BE2; | |
| font-size: 2em; | |
| margin-top: 0; | |
| margin-bottom: 30px; | |
| } | |
| #input-container { | |
| display: flex; | |
| flex-wrap: wrap; | |
| justify-content: space-between; | |
| gap: 15px; | |
| margin-bottom: 25px; | |
| } | |
| .number-input { | |
| flex-basis: calc(50% - 8px); /* Two columns with gap */ | |
| padding: 15px; | |
| border: 1px solid #ddd; | |
| border-radius: 8px; | |
| box-sizing: border-box; | |
| font-size: 1.2em; | |
| text-align: center; | |
| } | |
| button { | |
| width: 100%; | |
| padding: 20px; | |
| background-color: transparent; | |
| color: #8A2BE2; | |
| border: 1px solid #D1C4E9; | |
| border-radius: 8px; | |
| cursor: pointer; | |
| font-size: 1.5em; | |
| font-weight: bold; | |
| transition: background-color 0.3s, color 0.3s; | |
| } | |
| button:hover { | |
| background-color: #8A2BE2; | |
| color: #ffffff; | |
| } | |
| #result-container { | |
| margin-top: 25px; | |
| padding: 20px; | |
| background-color: #F3E5F5; | |
| border-radius: 8px; | |
| min-height: 80px; | |
| display: flex; | |
| flex-direction: column; | |
| justify-content: center; | |
| align-items: center; | |
| } | |
| #result-label { | |
| font-size: 1rem; | |
| color: #6A1B9A; | |
| font-weight: normal; | |
| } | |
| #result { | |
| font-size: 2.5em; | |
| font-weight: bold; | |
| color: #6A1B9A; | |
| } | |
| .error { | |
| font-size: 1.2rem ; | |
| color: #d9534f ; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <h1> Dynamic Average Calculator</h1> | |
| <div id="input-container"> | |
| </div> | |
| <button onclick="calculateAverage()">Calculate Average</button> | |
| <div id="result-container"> | |
| <span id="result-label"></span> | |
| <span id="result"></span> | |
| </div> | |
| </div> | |
| <script> | |
| const inputContainer = document.getElementById('input-container'); | |
| const resultSpan = document.getElementById('result'); | |
| const resultLabel = document.getElementById('result-label'); | |
| const MAX_INPUTS = 40; | |
| // Function to create a new input field | |
| function createInput() { | |
| const input = document.createElement('input'); | |
| input.type = 'number'; | |
| input.className = 'number-input'; | |
| const count = inputContainer.getElementsByTagName('input').length + 1; | |
| input.placeholder = `Num ${count}`; | |
| return input; | |
| } | |
| // Function to add more input fields if needed | |
| function addInputsIfNeeded() { | |
| const inputs = inputContainer.getElementsByClassName('number-input'); | |
| if (inputs.length >= MAX_INPUTS) { | |
| return; // Stop if max limit is reached | |
| } | |
| // Trigger to add more fields: if the last two fields have values | |
| if (inputs.length >= 2 && inputs[inputs.length - 1].value !== '' && inputs[inputs.length - 2].value !== '') { | |
| inputContainer.appendChild(createInput()); | |
| inputContainer.appendChild(createInput()); | |
| } | |
| } | |
| // Main calculation function | |
| function calculateAverage() { | |
| const inputs = inputContainer.getElementsByClassName('number-input'); | |
| let sum = 0; | |
| let count = 0; | |
| for (let i = 0; i < inputs.length; i++) { | |
| if (inputs[i].value !== '') { | |
| sum += parseFloat(inputs[i].value); | |
| count++; | |
| } | |
| } | |
| resultSpan.classList.remove('error'); | |
| if (count === 0) { | |
| resultLabel.textContent = ''; | |
| resultSpan.textContent = 'Enter at least one number.'; | |
| resultSpan.classList.add('error'); | |
| return; | |
| } | |
| const average = sum / count; | |
| resultLabel.textContent = `Average of ${count} numbers`; | |
| resultSpan.textContent = average.toFixed(2); | |
| } | |
| // Event listener for dynamic input creation | |
| inputContainer.addEventListener('input', addInputsIfNeeded); | |
| // Initialize the first 4 inputs on page load | |
| function initialize() { | |
| inputContainer.appendChild(createInput()); | |
| inputContainer.appendChild(createInput()); | |
| inputContainer.appendChild(createInput()); | |
| inputContainer.appendChild(createInput()); | |
| } | |
| initialize(); | |
| </script> | |
| </body> | |
| </html> |