-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
153 lines (117 loc) · 4.06 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
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
/**
*
* @param {Array[Number]} height
* @param {Number} weight
* @returns
*/
// the height parameter takes in height as [feet, inches]
function calculateBMI(height, weight) {
const heightAsMeters = (height[0] * 0.3048) + (height[1] * 0.0254);
const bmi = weight / (heightAsMeters * heightAsMeters);
return parseFloat(bmi.toFixed(1));
}
function convertMetersToFeetAndInches(meters) {
const feet = Math.floor(meters * 3.28084);
const inches = Math.round((meters * 3.28084 - feet) * 12);
return [feet, inches];
}
function convertDecimalFeetToFeetAndInches(decimalFeet) {
const feet = Math.floor(decimalFeet);
const inches = Math.round((decimalFeet - feet) * 12);
if (inches === 12) {
return [feet + 1, 0]; // if inches equals 12, add 1 to feet and set inches to 0
} else {
return [feet, inches];
}
}
function convertPoundsToKgs(pounds) {
const kgs = pounds * 0.45359237;
return kgs;
}
const allInputs = document.querySelectorAll("input");
allInputs.forEach((input) => {
input.addEventListener("keypress", (e) => {
const firstDotIndex = e.target.value.indexOf(".");
const hasOnePeriod = firstDotIndex !== -1 && e.target.value.indexOf(".", firstDotIndex + 1) === -1;
if (hasOnePeriod && e.key === '.') {
e.preventDefault(); // if it already has one decimal point, the next period (".") should be ignored.
} else {
if (e.key === '.') {
// continue the process as decimals can be entered.
} else {
if (/\d/.test(Number(e.key)) === false) {
e.preventDefault();
}
}
}
});
});
const calculateBtn = document.querySelector(".btn__calculate");
const calculateItem = document.querySelector(".calculator__score");
const scoreSpan = document.querySelector(".calculator__score .item__score");
const heightInput = document.querySelector(".input__height");
const weightInput = document.querySelector(".input__weight");
const heightTypeSelectors = document.querySelectorAll(".types__height");
const weightTypeSelectors = document.querySelectorAll(".types__weight");
let currentHeightUnit = 'm'; // 'm', 'f'
let currentWeightUnit = 'k'; // 'k', 'p'
heightTypeSelectors.forEach((selector) => {
selector.addEventListener("click", () => {
heightTypeSelectors.forEach(s => s.classList.remove("type__active"));
selector.classList.add("type__active");
currentHeightUnit = selector.classList[2].replace("type__", "");;
});
});
weightTypeSelectors.forEach((selector) => {
selector.addEventListener("click", () => {
weightTypeSelectors.forEach(s => s.classList.remove("type__active"));
selector.classList.add("type__active");
currentWeightUnit = selector.classList[2].replace("type__", "");
});
});
calculateBtn.addEventListener("click", () => {
let height = heightInput.value;
let weight = weightInput.value;
if (!heightInput.value) height = "0";
if (!weightInput.value) weight = "0";
if (height.endsWith(".")) {
height += "0";
}
if (weight.endsWith(".")) {
weight += "0";
}
height = Number(height);
weight = Number(weight);
// if height is in metres
if (currentHeightUnit === 'm') {
let score;
const feetInches = convertMetersToFeetAndInches(height);
// if weight is in kg
if (currentWeightUnit === 'k') {
score = calculateBMI(feetInches, weight);
}
// if weight is in pounds (lbs)
if(currentWeightUnit === 'p') {
const kgs = convertPoundsToKgs(weight);
score = calculateBMI(feetInches, kgs);
}
scoreSpan.textContent = score;
calculateItem.classList.add("visible");
}
// if height is in feet (inches)
if(currentHeightUnit === 'f') {
let score;
const feetInches = convertDecimalFeetToFeetAndInches(height);
// if weight is in kg
if(currentWeightUnit === 'k') {
score = calculateBMI(feetInches, weight);
}
// if weight is in pounds (lbs)
if(currentWeightUnit === 'p') {
const kgs = convertPoundsToKgs(weight);
score = calculateBMI(feetInches, kgs);
}
scoreSpan.textContent = score;
calculateItem.classList.add("visible");
}
});