|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="en"> |
3 | 3 | <head> |
4 | | - <meta charset="UTF-8"> |
5 | | - <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
6 | | - <meta http-equiv="X-UA-Compatible" content="ie=edge"> |
7 | | - <link rel="stylesheet" href="style.css"> |
8 | | - <title>BMI Calculator</title> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 6 | + <meta http-equiv="X-UA-Compatible" content="ie=edge" /> |
| 7 | + <title>BMI Calculator</title> |
| 8 | + <style> |
| 9 | + /* Simple styles for the form and container */ |
| 10 | + .container { |
| 11 | + max-width: 400px; |
| 12 | + margin: 50px auto; |
| 13 | + padding: 20px; |
| 14 | + font-family: Arial, sans-serif; |
| 15 | + border: 1px solid #ccc; |
| 16 | + border-radius: 8px; |
| 17 | + } |
| 18 | + input[type="text"] { |
| 19 | + width: 100%; |
| 20 | + padding: 8px; |
| 21 | + margin-top: 5px; |
| 22 | + margin-bottom: 15px; |
| 23 | + box-sizing: border-box; |
| 24 | + border-radius: 4px; |
| 25 | + border: 1px solid #ccc; |
| 26 | + } |
| 27 | + button { |
| 28 | + padding: 10px 15px; |
| 29 | + border-radius: 4px; |
| 30 | + border: none; |
| 31 | + background-color: #007bff; |
| 32 | + color: white; |
| 33 | + cursor: pointer; |
| 34 | + font-size: 16px; |
| 35 | + } |
| 36 | + button:hover { |
| 37 | + background-color: #0056b3; |
| 38 | + } |
| 39 | + #results { |
| 40 | + margin-top: 20px; |
| 41 | + font-weight: bold; |
| 42 | + color: #333; |
| 43 | + } |
| 44 | + #weight-guide h3 { |
| 45 | + margin-bottom: 6px; |
| 46 | + } |
| 47 | + </style> |
9 | 48 | </head> |
10 | 49 | <body> |
11 | | - <div class="container"> |
| 50 | + <div class="container"> |
12 | 51 | <h1>BMI Calculator</h1> |
13 | | - <form> |
14 | | - <p><label><b>Height(cm): </b></label><input type="text" id="height"></p> |
15 | | - <p><label><b>Weight(kg): </b></label><input type="text" id="weight"></p> |
16 | | - <button>Calculate</button> |
17 | | - <div id="results"></div> |
18 | | - <div id="weight-guide"> |
19 | | - <h3>BMI Weight Guide</h3> |
20 | | - <p>Under Weight = Less than 18.6</p> |
21 | | - <p>Normal Range = 18.6 and 24.9</p> |
22 | | - <p>Overweight = Greater than 24.9</p> |
23 | | - </div> |
| 52 | + <form id="bmi-form"> |
| 53 | + <label><b>Height (cm):</b></label> |
| 54 | + <input type="text" id="height" placeholder="Enter height in cm" /> |
| 55 | + <label><b>Weight (kg):</b></label> |
| 56 | + <input type="text" id="weight" placeholder="Enter weight in kg" /> |
| 57 | + <button type="submit">Calculate</button> |
| 58 | + <div id="results"></div> |
| 59 | + <div id="weight-guide"> |
| 60 | + <h3>BMI Weight Guide</h3> |
| 61 | + <p>Under Weight = Less than 18.6</p> |
| 62 | + <p>Normal Range = 18.6 and 24.9</p> |
| 63 | + <p>Overweight = Greater than 24.9</p> |
| 64 | + </div> |
24 | 65 | </form> |
25 | | - </div> |
| 66 | + </div> |
| 67 | + |
| 68 | + <script> |
| 69 | + document.getElementById('bmi-form').addEventListener('submit', function (event) { |
| 70 | + event.preventDefault(); |
| 71 | + |
| 72 | + const heightInput = document.getElementById('height').value.trim(); |
| 73 | + const weightInput = document.getElementById('weight').value.trim(); |
| 74 | + const resultsDiv = document.getElementById('results'); |
| 75 | + |
| 76 | + const height = parseFloat(heightInput); |
| 77 | + const weight = parseFloat(weightInput); |
| 78 | + |
| 79 | + if ( |
| 80 | + isNaN(height) || |
| 81 | + isNaN(weight) || |
| 82 | + height <= 0 || |
| 83 | + weight <= 0 |
| 84 | + ) { |
| 85 | + resultsDiv.textContent = 'Please enter valid positive numbers for height and weight.'; |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const heightInMeters = height / 100; |
| 90 | + const bmi = weight / (heightInMeters * heightInMeters); |
| 91 | + resultsDiv.textContent = `Your BMI is ${bmi.toFixed(2)}`; |
| 92 | + }); |
| 93 | + </script> |
26 | 94 | </body> |
27 | | -<script src="script.js"></script> |
28 | 95 | </html> |
0 commit comments