-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
137 lines (118 loc) Β· 5.5 KB
/
script.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Constants
const POD_WEIGHT_GRAMS = 3;
const POD_DIAMETER_CM = 3.5;
const POD_HEIGHT_CM = 3;
const POD_BASE_DIAMETER_CM = 2;
// Reference weights and volumes for comparisons
const COMPARISONS = {
weights: [
{ name: "iPhone 14 Pro Max", weight: 240, emoji: "π±" },
{ name: "basketball", weight: 650, emoji: "π" },
{ name: "house cat", weight: 4000, emoji: "π±" },
{ name: "microwave", weight: 12000, emoji: "π‘" },
{ name: "hamster", weight: 120, emoji: "πΉ" },
{ name: "pair of jeans", weight: 800, emoji: "π" },
{ name: "laptop", weight: 2000, emoji: "π»" },
{ name: "guitar", weight: 3000, emoji: "πΈ" },
{ name: "car tire", weight: 10000, emoji: "π" },
{ name: "standard brick", weight: 2300, emoji: "π§±" },
{ name: "bowling ball", weight: 7000, emoji: "π³" },
{ name: "bag of flour", weight: 1000, emoji: "πΎ" },
{ name: "bicycle", weight: 15000, emoji: "π²" },
{ name: "medium-sized dog", weight: 25000, emoji: "π" },
{ name: "office chair", weight: 13000, emoji: "πΊ" },
{ name: "mini fridge", weight: 20000, emoji: "βοΈ" },
{ name: "acoustic guitar", weight: 2500, emoji: "πΈ" },
{ name: "car battery", weight: 18000, emoji: "π" },
{ name: "beach umbrella", weight: 3500, emoji: "β±οΈ" }
],
volumes: [
{ name: "coffee cup", volume: 250, emoji: "β" },
{ name: "shoebox", volume: 5000, emoji: "π" },
{ name: "carry-on suitcase", volume: 40000, emoji: "π§³" },
{ name: "bathtub", volume: 200000, emoji: "π" },
{ name: "water bottle", volume: 500, emoji: "πΆ" },
{ name: "basketball", volume: 7300, emoji: "π" },
{ name: "microwave oven", volume: 25000, emoji: "π‘" },
{ name: "filing cabinet drawer", volume: 30000, emoji: "ποΈ" },
{ name: "kitchen sink", volume: 35000, emoji: "π°" },
{ name: "mini fridge", volume: 100000, emoji: "βοΈ" },
{ name: "washing machine drum", volume: 60000, emoji: "π§Ί" },
{ name: "recycling bin", volume: 120000, emoji: "β»οΈ" },
{ name: "office desk", volume: 180000, emoji: "πͺ" },
{ name: "standard mailbox", volume: 15000, emoji: "π«" },
{ name: "guitar case", volume: 20000, emoji: "πΈ" },
{ name: "beach cooler", volume: 45000, emoji: "ποΈ" },
{ name: "storage tote", volume: 50000, emoji: "π¦" },
{ name: "trash can", volume: 75000, emoji: "ποΈ" },
{ name: "garden wheelbarrow", volume: 90000, emoji: "π±" }
]
};
// Calculate pod volume (cone frustum formula)
function calculatePodVolume() {
const r1 = POD_DIAMETER_CM / 2;
const r2 = POD_BASE_DIAMETER_CM / 2;
const h = POD_HEIGHT_CM;
return (Math.PI * h / 3) * (r1*r1 + r2*r2 + r1*r2);
}
function formatNumber(num) {
if (num >= 1000000) {
return (num / 1000000).toFixed(1) + 'M';
} else if (num >= 1000) {
return (num / 1000).toFixed(1) + 'K';
}
return num.toFixed(1);
}
function getRandomComparisons(amount, items, count = 2) {
// Filter items that are appropriate for the amount
const appropriateItems = items.filter(item => {
const value = item.weight || item.volume;
return amount >= value * 0.1 && amount <= value * 100;
});
if (appropriateItems.length === 0) return [items[0]];
// Shuffle and take first 2 items
return appropriateItems
.sort(() => Math.random() - 0.5)
.slice(0, count);
}
function generateComparisonMessage(amount, comparison, type = 'weight') {
const value = comparison[type] || comparison.volume;
const ratio = (amount / value).toFixed(1);
const plural = ratio === "1.0" ? "" : "s";
return `${ratio} ${comparison.emoji} ${comparison.name}${plural}`;
}
function updateCalculations() {
const podsPerDay = parseInt(document.getElementById('pods-per-day').value);
document.getElementById('pods-value').textContent = podsPerDay;
// Calculate weights
const gramsPerDay = podsPerDay * POD_WEIGHT_GRAMS;
const gramsPerMonth = gramsPerDay * 30;
const gramsPerYear = gramsPerDay * 365;
// Calculate volumes
const volumePerPod = calculatePodVolume();
const volumePerYear = volumePerPod * podsPerDay * 365;
// Get random comparisons
const weightComparisons = getRandomComparisons(gramsPerYear, COMPARISONS.weights);
const volumeComparisons = getRandomComparisons(volumePerYear, COMPARISONS.volumes);
// Generate message
const yearlyWeightKg = gramsPerYear / 1000;
const weightMessages = weightComparisons
.map(comp => generateComparisonMessage(gramsPerYear, comp, 'weight'))
.join(' or ');
const volumeMessages = volumeComparisons
.map(comp => generateComparisonMessage(volumePerYear, comp, 'volume'))
.join(' or ');
const message = `
You're adding ${formatNumber(gramsPerMonth)}g of plastic per month, or ${formatNumber(yearlyWeightKg)}kg per year.
That's equivalent to ${weightMessages}!
Over a year, these pods would take up ${formatNumber(volumePerYear)}cmΒ³ in a landfill -
that's about the same as ${volumeMessages}!
`;
document.getElementById('results').innerHTML = message.split('\n').map(line =>
`<p>${line.trim()}</p>`
).join('');
}
// Event listeners
document.getElementById('pods-per-day').addEventListener('input', updateCalculations);
// Initial calculation
updateCalculations();