-
Notifications
You must be signed in to change notification settings - Fork 0
/
IAmNTimesAsOldAsYou.html
100 lines (90 loc) · 4.31 KB
/
IAmNTimesAsOldAsYou.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>I'm N Times as Old as You</title>
</head>
<body>
<h1>I'm N Times as Old as You</h1>
Older birthdate (o): <input type="text" id="older"><br>
Younger birthdate (y): <input type="text" id="younger"><br>
Precision (p): <select id="precision">
<option value="y">Year</option>
<option value="m">Month</option>
<option value="d">Day</option>
</select>
<button onClick="calculate()">Caclulate</button><br>
<div id="result"></div>
<script>
// Function to parse the date based on precision
function parseDate(date, precision) {
var parts = date.split('-');
var year = parseInt(parts[0]);
var month = precision === 'y' ? 0 : parseInt(parts[1]) - 1;
var day = precision === 'd' ? parseInt(parts[2]) : 1;
return new Date(year, month, day);
}
function parseInput() {
var olderDateInput = document.getElementById('older').value;
var youngerDateInput = document.getElementById('younger').value;
var precision = document.getElementById('precision').value;
return {
olderDate: parseDate(olderDateInput, precision),
youngerDate: parseDate(youngerDateInput, precision),
precision: precision
};
}
function calculateAgeAndMultiplier(olderDate, youngerDate, precision) {
var currentDate = new Date();
if (precision === 'y') {
currentDate.setMonth(0);
currentDate.setDate(1);
} else if (precision === 'm') {
currentDate.setDate(1);
}
var olderAge = (currentDate - olderDate) / (1000 * 60 * 60 * 24 * 365.25);
var youngerAge = (currentDate - youngerDate) / (1000 * 60 * 60 * 24 * 365.25);
var multiplier = olderAge / youngerAge;
return { olderAge, youngerAge, multiplier };
}
function calculateFutureNValues(olderAge, youngerAge, olderDate, multiplier) {
var X = Math.floor(multiplier);
var futureNValues = [];
for (var N = 2; N <= X; N++) {
var targetMultiplier = N;
var targetOlderAge = olderAge + (olderAge - (targetMultiplier * youngerAge)) / (targetMultiplier - 1);
var targetDate = new Date(olderDate.getTime() + targetOlderAge * 1000 * 60 * 60 * 24 * 365.25);
futureNValues.push({ N, targetDate });
}
return futureNValues;
}
function displayResult(olderAge, youngerAge, multiplier, futureNValues) {
var resultDiv = document.getElementById('result');
resultDiv.innerHTML = `Age difference: ${olderAge.toFixed(1)} - ${youngerAge.toFixed(1)} = ${(olderAge - youngerAge).toFixed(1)} years<br>`;
resultDiv.innerHTML += `I am ${multiplier.toFixed(1)} times as Old as You<br>`;
resultDiv.innerHTML += '<h3>Future N Values:</h3>';
futureNValues.forEach(value => {
resultDiv.innerHTML += `N = ${value.N}: ${value.targetDate.toDateString()}<br>`;
});
}
function calculate() {
var { olderDate, youngerDate, precision } = parseInput();
var { olderAge, youngerAge, multiplier } = calculateAgeAndMultiplier(olderDate, youngerDate, precision);
var futureNValues = calculateFutureNValues(olderAge, youngerAge, olderDate, multiplier);
displayResult(olderAge, youngerAge, multiplier, futureNValues);
}
// Function to retrieve parameters from URL and set values
function getParams() {
var params = new URLSearchParams(window.location.search);
document.getElementById('older').value = params.get('o') || '';
document.getElementById('younger').value = params.get('y') || '';
document.getElementById('precision').value = params.get('p') || 'y';
calculate();
}
// Call getParams on page load
window.onload = getParams;
</script>
</body>
</html>