-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (79 loc) · 3.31 KB
/
index.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
// Conversion rates
const meterToFeet = 3.281;
const literToGallon = 0.264;
const kilogramToPound = 2.204;
// Selecting elements from the HTML document
const convertBtn = document.getElementById("convert-btn");
const inputEl = document.getElementById("input-el");
const lengthEl = document.getElementById("length-el");
const volumeEl = document.getElementById("volume-el");
const massEl = document.getElementById("mass-el");
const modeToggleBtn = document.getElementById("mode-toggle");
const bodyEl = document.body;
const containerEl = document.querySelector(".main");
const sections = document.querySelectorAll(".section");
const lengthEl1 = document.getElementById("length-el");
const volumeEl1 = document.getElementById("volume-el");
const massEl1 = document.getElementById("mass-el");
const h3Elements = document.querySelectorAll("h3");
// Adding an event listener to the convert button
convertBtn.addEventListener("click", () => {
const baseValue = Number(inputEl.value);
// Calculating length conversion
const lengthFeet = baseValue * meterToFeet;
const lengthMeter = baseValue / meterToFeet;
const volumeLiter = baseValue * literToGallon;
const volumeGallon = baseValue / literToGallon;
const massKilograms = baseValue * kilogramToPound;
const massPound = baseValue / kilogramToPound;
// Displaying length conversion results
lengthEl.textContent = `${baseValue} meters = ${lengthFeet.toFixed(3)} feet | ${baseValue} feet = ${lengthMeter.toFixed(3)} meters`;
volumeEl.textContent = `${baseValue} liters = ${volumeLiter.toFixed(3)} gallons | ${baseValue} gallons = ${volumeGallon.toFixed(3)} liters`;
massEl.textContent = `${baseValue} kilos = ${massKilograms.toFixed(3)} pounds | ${baseValue} pounds = ${massPound.toFixed(3)} kilos`;
});
// Tracking the current mode (true for dark mode, false for light mode)
let isDarkMode = false;
// Function to toggle between dark mode and light mode
function toggleMode() {
isDarkMode = !isDarkMode;
if (isDarkMode) {
bodyEl.style.backgroundColor = "#1C1D21";
containerEl.style.backgroundColor = "#1F2937";
lengthEl1.style.backgroundColor = "#273549";
volumeEl1.style.backgroundColor = "#273549";
massEl1.style.backgroundColor = "#273549";
sections.forEach((section) => {
section.style.backgroundColor = "#273549";
section.style.color = "#FFF";
});
h3Elements.forEach((h3) => {
h3.style.color = "#CCC1FF";
});
lengthEl1.style.color = "#FFF";
volumeEl1.style.color = "#FFF";
massEl1.style.color = "#FFF";
} else {
bodyEl.style.backgroundColor = "#C9E3E4";
containerEl.style.backgroundColor = "";
lengthEl1.style.backgroundColor = "";
volumeEl1.style.backgroundColor = "";
massEl1.style.backgroundColor = "";
sections.forEach((section) => {
section.style.backgroundColor = "";
section.style.color = "";
});
h3Elements.forEach((h3) => {
h3.style.color = "";
});
lengthEl1.style.color = "";
volumeEl1.style.color = "";
massEl1.style.color = "";
}
}
// Mode toggle event listener
modeToggleBtn.addEventListener("click", toggleMode);
// Adding an input event listener for input validation
inputEl.addEventListener("input", () => {
// Remove any non-digit characters from the input value
inputEl.value = inputEl.value.replace(/\D/g, "");
});