-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
58 lines (58 loc) · 2.22 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
"use strict";
const billAmountInput = document.getElementById('bill');
const tipPercentInput = document.getElementById('tip');
const currencySelector = document.getElementById('currency');
window.onload = function () {
billAmountInput.value = localStorage.getItem('billAmount') || '';
tipPercentInput.value = localStorage.getItem('tipPercent') || '';
currencySelector.value = localStorage.getItem('currency') || 'USD';
billAmountInput.addEventListener('input', function () {
localStorage.setItem('billAmount', this.value);
});
tipPercentInput.addEventListener('input', function () {
localStorage.setItem('tipPercent', this.value);
});
currencySelector.addEventListener('change', function () {
localStorage.setItem('currency', this.value);
});
};
function calculateTip() {
const billAmount = Number(billAmountInput.value);
const tipPercent = Number(tipPercentInput.value);
if (isNaN(billAmount) || isNaN(tipPercent)) {
alert("Please enter valid numbers for both fields.");
return;
}
const tipAmount = billAmount * (tipPercent / 100);
const currencySymbol = getCurrencySymbol(currencySelector.value);
const resultElement = document.getElementById('result');
resultElement.innerHTML = `Tip Amount: ${currencySymbol}${tipAmount.toFixed(2)}`;
}
function getCurrencySymbol(currency) {
switch (currency) {
case "USD":
return "$";
case "EUR":
return "€";
case "GBP":
return "£";
case "CHF":
return "₣";
default:
return "$";
}
}
document.addEventListener('DOMContentLoaded', function () {
const themeSelector = document.getElementById('theme-selector');
themeSelector.addEventListener('change', function () {
document.body.classList.remove('theme-default', 'theme-dark');
document.body.classList.add(this.value);
localStorage.setItem('selectedTheme', this.value);
});
const savedTheme = localStorage.getItem('selectedTheme');
if (savedTheme) {
document.body.classList.remove('theme-default', 'theme-dark');
document.body.classList.add(savedTheme);
themeSelector.value = savedTheme;
}
});