-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
224 lines (189 loc) · 6.08 KB
/
Copy pathapp.js
File metadata and controls
224 lines (189 loc) · 6.08 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
console.log("Loyalty Points Simulator is loading...");
// Simple FX table (just examples for now)
const fxRates = {
US: 1, // USD base
EU: 1.1, // EUR → USD example rate
UK: 1.25, // GBP → USD
JP: 0.007 // JPY → USD
};
// Tier multipliers (applied on top of earn rate)
const tierMultipliers = {
Silver: 1.0,
Gold: 1.25,
Platinum: 1.5
};
// Assumed point value in USD
const POINT_VALUE_USD = 0.01;
// Formatting helpers
function formatNumber(value) {
return Number.isInteger(value) ? value.toString() : value.toFixed(2);
}
function formatPercent(value) {
return `${formatNumber(value)}%`;
}
function formatCurrency(value) {
return `$${value.toFixed(2)}`;
}
function getRegionLabel(region) {
switch (region) {
case "US":
return "United States (USD)";
case "EU":
return "Europe (EUR)";
case "UK":
return "United Kingdom (GBP)";
case "JP":
return "Japan (JPY)";
default:
return region;
}
}
// Summary rendering
function renderSummary({ region, tier, points, redemptionValueUSD }) {
const summary = document.getElementById("summary");
summary.innerHTML = "";
const badge = document.createElement("div");
badge.className = "summary-badge summary-badge-ok";
badge.innerHTML = `
<span class="count">${formatNumber(points)}</span> point${points === 1 ? "" : "s"}
· Redemption value: <strong>${formatCurrency(redemptionValueUSD)}</strong>
· Region: ${region} · Tier: ${tier}
`;
summary.appendChild(badge);
}
function renderErrorSummary(message) {
const summary = document.getElementById("summary");
summary.innerHTML = `
<div class="summary-badge summary-badge-fail">
${message}
</div>
`;
}
// Main calculation
function calculatePoints() {
const region = document.getElementById("region").value;
const tier = document.getElementById("tier").value;
const amountRaw = document.getElementById("amount").value;
const rateRaw = document.getElementById("rate").value;
const status = document.getElementById("calc-status");
const amount = Number(amountRaw);
const rate = Number(rateRaw);
if (!amountRaw || isNaN(amount) || amount <= 0) {
renderErrorSummary("Enter a valid transaction amount.");
status.textContent = "Amount must be a positive number.";
resetDetails();
return;
}
if (!rateRaw || isNaN(rate) || rate < 0) {
renderErrorSummary("Enter a valid earn rate.");
status.textContent = "Earn rate must be zero or positive.";
resetDetails();
return;
}
const fx = fxRates[region] ?? 1;
const convertedAmount = amount * fx;
const tierMultiplier = tierMultipliers[tier] || 1;
const effectiveRate = rate * tierMultiplier;
const points = convertedAmount * (effectiveRate / 100);
const redemptionValue = points * POINT_VALUE_USD;
status.textContent = "Calculation complete.";
renderSummary({
region,
tier,
points,
redemptionValueUSD: redemptionValue,
});
renderDetails({
region,
fx,
amount,
convertedAmount,
tier,
rate,
tierMultiplier,
effectiveRate,
points,
redemptionValue,
});
}
function resetDetails() {
document.getElementById("fx-line").textContent =
"Choose a region and amount to see how spend is normalized to USD.";
document.getElementById("tier-line").textContent =
"Tier multipliers stack on top of the base earn rate.";
document.getElementById("points-line").textContent =
"Points earned represent a future redemption liability for the program.";
document.getElementById("raw-output").textContent = "No calculation yet.";
}
function renderDetails({
region,
fx,
amount,
convertedAmount,
tier,
rate,
tierMultiplier,
effectiveRate,
points,
redemptionValue,
}) {
const regionLabel = getRegionLabel(region);
const fxLine = document.getElementById("fx-line");
fxLine.innerHTML = `
<strong>${regionLabel}</strong><br />
Local amount: ${formatNumber(amount)} · FX: ${fx}<br />
Normalized spend: <strong>${formatCurrency(convertedAmount)}</strong> (USD)
`;
const tierLine = document.getElementById("tier-line");
tierLine.innerHTML = `
Base earn rate: ${formatPercent(rate)} · Tier: <strong>${tier}</strong><br />
Tier multiplier: ${tierMultiplier}x → Effective rate:
<strong>${formatPercent(effectiveRate)}</strong>
`;
const pointsLine = document.getElementById("points-line");
pointsLine.innerHTML = `
Points earned: <strong>${formatNumber(points)}</strong><br />
Assumed point value: ${formatCurrency(POINT_VALUE_USD)} per point<br />
Estimated liability: <strong>${formatCurrency(redemptionValue)}</strong> (USD)
`;
const raw = [
`Region: ${region} (${regionLabel})`,
`FX rate: ${fx}`,
`Local transaction amount: ${amount}`,
`Converted amount (USD): ${formatCurrency(convertedAmount)}`,
`Tier: ${tier}`,
`Base earn rate: ${formatPercent(rate)}`,
`Tier multiplier: ${tierMultiplier}x`,
`Effective earn rate: ${formatPercent(effectiveRate)}`,
`Points earned: ${formatNumber(points)}`,
`Redemption value (USD): ${formatCurrency(redemptionValue)}`,
].join("\n");
document.getElementById("raw-output").textContent = raw;
}
// Example scenario loader
function loadExampleScenario() {
document.getElementById("region").value = "EU";
document.getElementById("tier").value = "Gold";
document.getElementById("amount").value = "150";
document.getElementById("rate").value = "4";
document.getElementById("calc-status").textContent = "Example scenario loaded. Click Calculate Points.";
resetDetails();
const summary = document.getElementById("summary");
summary.innerHTML = `
<div class="summary-badge summary-badge-idle">
Ready to calculate the example scenario.
</div>
`;
}
// Wire up
document.addEventListener("DOMContentLoaded", () => {
document
.getElementById("calculateBtn")
.addEventListener("click", calculatePoints);
const exampleBtn = document.getElementById("load-example");
if (exampleBtn) {
exampleBtn.addEventListener("click", loadExampleScenario);
}
// Optional: pre-load example on first load
loadExampleScenario();
});