-
-
Notifications
You must be signed in to change notification settings - Fork 379
/
Copy pathscript.js
113 lines (93 loc) · 3 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
const $$ = (selector) => document.querySelectorAll(selector);
const $ = (selector) => document.querySelector(selector);
const SELECTORS = {
HEIGHT: '#height',
WEIGHT: '#weight',
RESULT: '#result',
CALCULATE_BTN: '#calculateBtn',
THEME_TOGGLE_BTN: '#themeToggle',
};
const STORAGE_KEYS = {
DARK_MODE: 'darkMode',
};
const BMI_CATEGORIES = {
UNDERWEIGHT: { max: 18.5, label: 'Underweight' },
NORMAL: { max: 25, label: 'Normal weight' },
OVERWEIGHT: { max: 30, label: 'Overweight' },
OBESE: { max: Infinity, label: 'Obese' },
};
const ERROR_MESSAGES = {
INVALID_INPUT: "Please enter valid height and weight.",
};
class BMICalculator {
constructor() {
this.initSelectors();
this.bindEvents();
this.initTheme();
}
initSelectors() {
this.heightInput = $(SELECTORS.HEIGHT);
this.weightInput = $(SELECTORS.WEIGHT);
this.resultDiv = $(SELECTORS.RESULT);
this.calculateBtn = $(SELECTORS.CALCULATE_BTN);
this.themeToggleBtn = $(SELECTORS.THEME_TOGGLE_BTN);
}
bindEvents() {
this.calculateBtn.addEventListener('click', this.calculateBMI.bind(this));
this.themeToggleBtn.addEventListener('click', this.toggleDarkMode.bind(this));
}
initTheme() {
const isDarkMode = localStorage.getItem(STORAGE_KEYS.DARK_MODE) === 'enabled';
if (isDarkMode) {
document.documentElement.setAttribute('data-theme', isDarkMode ? 'dark' : 'light');
document.body.classList.add('dark-mode');
}
this.updateThemeToggleButton(isDarkMode);
}
calculateBMI() {
const height = this.getHeight();
const weight = this.getWeight();
if (this.isValidInput(height, weight)) {
const bmi = this.computeBMI(weight, height);
const category = this.determineBMICategory(bmi);
this.displayResult(bmi, category);
} else {
this.displayError();
}
}
getHeight() {
return parseFloat(this.heightInput.value) / 100;
}
getWeight() {
return parseFloat(this.weightInput.value);
}
isValidInput(height, weight) {
return !isNaN(height) && !isNaN(weight) && height > 0 && weight > 0;
}
computeBMI(weight, height) {
return weight / (height * height);
}
determineBMICategory(bmi) {
for (const category of Object.values(BMI_CATEGORIES)) {
if (bmi < category.max) {
return category.label;
}
}
}
displayResult(bmi, category) {
this.resultDiv.innerHTML = `Your BMI is: ${bmi.toFixed(2)}<br>Category: ${category}`;
}
displayError() {
this.resultDiv.textContent = ERROR_MESSAGES.INVALID_INPUT;
}
toggleDarkMode() {
const isDarkMode = document.body.classList.toggle('dark-mode');
this.updateThemeToggleButton(isDarkMode);
document.documentElement.setAttribute('data-theme', isDarkMode ? 'dark' : 'light');
localStorage.setItem(STORAGE_KEYS.DARK_MODE, isDarkMode ? 'enabled' : 'disabled');
}
updateThemeToggleButton(isDarkMode) {
this.themeToggleBtn.style.color = isDarkMode ? '#fff' : '#333';
}
}
document.addEventListener('DOMContentLoaded', () => new BMICalculator());