-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
86 lines (73 loc) · 2.79 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
function calculateBMI() {
const age = document.getElementById('age').value;
let height = document.getElementById('height').value;
let weight = document.getElementById('weight').value;
const heightUnit = document.getElementById('height-unit').value;
const weightUnit = document.getElementById('weight-unit').value;
const gender = document.querySelector('input[name="gender"]:checked').value;
if (heightUnit === 'in') {
height = height * 2.54; // convert inches to cm
}
if (weightUnit === 'lb') {
weight = weight * 0.453592; // convert pounds to kg
}
height = height / 100; // convert cm to meters
const bmi = weight / (height * height);
const bmiValue = bmi.toFixed(1);
const indicator = document.getElementById('bmi-indicator');
const bmiResult = document.getElementById('bmi-value');
bmiResult.textContent = bmiValue;
let bmiCategory;
if (bmi < 17.1) {
indicator.style.left = `${(bmi / 17.1) * 33.33}%`;
bmiCategory = "Underweight 10 BMI - 17.1 BMI";
} else if (bmi < 23.1) {
indicator.style.left = `${33.33 + ((bmi - 17.1) / (23.1 - 17.1)) * 33.33}%`;
bmiCategory = "Normal 17.1 BMI - 23.2 BMI";
} else {
indicator.style.left = `${66.66 + ((bmi - 23.1) / (38 - 23.1)) * 33.33}%`;
bmiCategory = "Overweight 23.2 BMI - 38 BMI";
}
let message = `Your BMI is ${bmiValue}<br>(${bmiCategory})`;
if (gender === 'male') {
if (bmi < 17.1) {
message += "<br>Consider gaining weight.";
} else if (bmi < 23.2) {
message += "<br>You are in a healthy weight range.";
} else {
message += "<br>Consider losing weight.";
}
} else if (gender === 'female') {
if (bmi < 17.1) {
message += "<br>Consider gaining weight.";
} else if (bmi < 23.2) {
message += "<br>You are in a healthy weight range.";
} else {
message += "<br>Consider losing weight.";
}
}
bmiResult.innerHTML = message;
}
// Get the theme toggle input element
const themeToggle = document.getElementById('theme-toggle');
// Get the body element
const body = document.body;
// Function to set the theme
function setTheme(isDarkMode) {
if (isDarkMode) {
body.classList.add('dark-theme');
themeToggle.checked = true;
} else {
body.classList.remove('dark-theme');
themeToggle.checked = false;
}
}
// Load the saved theme from localStorage
const savedTheme = localStorage.getItem('theme');
setTheme(savedTheme === 'dark');
// Save the theme when the toggle is clicked
themeToggle.addEventListener('change', () => {
const isDarkMode = themeToggle.checked;
setTheme(isDarkMode);
localStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
});