Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Tangent Formula Calculator and Height Unit option in BMI Calculator #1850

Merged
merged 10 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Calculators/BMI-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,20 @@ <h1>BMI Calculator</h1>
</div>
<div class="containerHW">
<div class="inputH">
<label for="heightUnit">Height Unit</label>
<select id="heightUnit" onchange="toggleHeightInput()">
<option value="cm">cm</option>
<option value="feet">feet</option>
</select>
</div>
<div class="inputH" id="heightCmInput">
<label for="height">Height(cm)</label>
<input type="number" id="height" required>
</div>
<div class="inputH" id="heightFeetInput" style="display: none;">
<label for="heightFeet">Height(feet)</label>
<input type="number" id="heightFeet" required>
</div>
<div class="inputW">
<label for="weight">Weight(kg)</label>
<input type="number" id="weight" required>
Expand Down
36 changes: 32 additions & 4 deletions Calculators/BMI-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,50 @@ var span = document.getElementsByClassName("close")[0];
// document.querySelector("#result").innerHTML = "00.00";

function calculate() {

var heightUnit = document.getElementById("heightUnit").value;
var heightValue;

if (heightUnit === "cm") {
heightValue = document.getElementById("height").value;
} else {
var heightFeet = document.getElementById("heightFeet").value;
heightValue = convertFeetToCm(heightFeet);
}

if (age.value == '' || height.value == '' || weight.value == '' || (male.checked == false && female.checked == false)) {
modal.style.display = "block";
modalText.innerHTML = 'ALL fields are required!';
} else if (!isPositiveNumber(height.value) || !isPositiveNumber(weight.value)) {
modal.style.display = "block";
modalText.innerHTML = 'Please enter valid positive values for height and weight!';
} else {
countBmi();
countBmi(heightValue);
}
}
function isPositiveNumber(value) {
return /^\d*\.?\d+$/.test(value) && parseFloat(value) > 0;
}
function countBmi() {
var p = [age.value, height.value, weight.value];
function convertFeetToCm(feet) {
return (feet * 30.48) ;
}
function toggleHeightInput() {
var heightUnit = document.getElementById("heightUnit").value;
var heightLabel = document.querySelector("label[for='height']");

if (heightUnit === "cm") {
document.getElementById("heightCmInput").style.display = "block";
document.getElementById("heightFeetInput").style.display = "none";
heightLabel.textContent = "Height(cm)";
} else {
document.getElementById("heightCmInput").style.display = "none";
document.getElementById("heightFeetInput").style.display = "block";
heightLabel.textContent = "Height(feet)";

}
}
function countBmi(heightValue) {
var p = [age.value, heightValue, weight.value];
if (male.checked) {
p.push("male");
} else if (female.checked) {
Expand Down Expand Up @@ -60,7 +89,6 @@ function countBmi() {

resultArea.style.display = "block";
document.querySelector(".comment").innerHTML = `You are <span id="comment">${result}</span>`;
// Update the result only after the calculation
document.querySelector("#result").innerHTML = bmi.toFixed(2);
}

Expand Down
4 changes: 3 additions & 1 deletion Calculators/BMI-Calculator/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,6 @@ button.calculate:hover {
.linkDownload {
display: none;
}
}
}