|
| 1 | +function calculateAge() { |
| 2 | + const birthdateInput = document.getElementById('birthdate'); |
| 3 | + const ageInYearsElement = document.getElementById('age-in-years'); |
| 4 | + const noveltyUnitsList = document.getElementById('novelty-units'); |
| 5 | + |
| 6 | + const birthdate = new Date(birthdateInput.value); |
| 7 | + const today = new Date(); |
| 8 | + |
| 9 | + const ageInMilliseconds = today - birthdate; |
| 10 | + const ageInSeconds = ageInMilliseconds / 1000; |
| 11 | + const ageInYears = ageInSeconds / (365 * 24 * 60 * 60); |
| 12 | + |
| 13 | + ageInYearsElement.textContent = `Your age in years: ${ageInYears.toFixed(2)}`; |
| 14 | + |
| 15 | + const planetaryYears = { |
| 16 | + 'Mercury': ageInYears / 0.2408467, |
| 17 | + 'Venus': ageInYears / 0.61519726, |
| 18 | + 'Mars': ageInYears / 1.8808158, |
| 19 | + 'Jupiter': ageInYears / 11.862615, |
| 20 | + 'Saturn': ageInYears / 29.447498, |
| 21 | + 'Uranus': ageInYears / 84.016846, |
| 22 | + 'Neptune': ageInYears / 164.79132 |
| 23 | + }; |
| 24 | + |
| 25 | + const fruitYears = { |
| 26 | + 'Apple': ageInYears / 80, |
| 27 | + 'Banana': ageInYears / 25, |
| 28 | + 'Carrot': ageInYears / 2, |
| 29 | + 'Grape': ageInYears / 60, |
| 30 | + 'Watermelon': ageInYears / 90 |
| 31 | + }; |
| 32 | + |
| 33 | + noveltyUnitsList.innerHTML = ''; |
| 34 | + |
| 35 | + for (const planet in planetaryYears) { |
| 36 | + const listItem = document.createElement('li'); |
| 37 | + listItem.textContent = `${planet}: ${planetaryYears[planet].toFixed(2)} years`; |
| 38 | + noveltyUnitsList.appendChild(listItem); |
| 39 | + } |
| 40 | + |
| 41 | + for (const fruit in fruitYears) { |
| 42 | + const listItem = document.createElement('li'); |
| 43 | + listItem.textContent = `${fruit}: ${fruitYears[fruit].toFixed(2)} years`; |
| 44 | + noveltyUnitsList.appendChild(listItem); |
| 45 | + } |
| 46 | +} |
0 commit comments