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

Update in the height's units #1865

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 7 additions & 5 deletions Calculators/BMI-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ <h1>BMI Calculator</h1>
</div>
<div class="containerHW">
<div class="inputH">
<label for="height">Height(cm)</label>
<label for="height">Height (cm)</label>
<input type="number" id="height" required>
<br>
<a style="font-size:x-small; font-weight: bold;" href="#" id="convertToFt">Convert to ft</a>
</div>
<div class="inputW">
<label for="weight">Weight(kg)</label>
<label for="weight">Weight (kg)</label>
<input type="number" id="weight" required>
</div>
</div>
</div>
<button class="calculate" id="submit" onclick="calculate();">Calculate BMI</button>
</div>
<div class="result">
Expand All @@ -56,7 +58,7 @@ <h1>BMI Calculator</h1>
<p id="modalText"></p>
</div>
</div>
<script src="script.js"></script>
<script src="script.js"></script>
</body>

</html>
</html>
20 changes: 17 additions & 3 deletions Calculators/BMI-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@ modalText = document.querySelector("#modalText");
var modal = document.getElementById("myModal");
var span = document.getElementsByClassName("close")[0];

// Remove the initial setting of the result
// document.querySelector("#result").innerHTML = "00.00";
// Event listener for the "Convert to ft" link
document.getElementById('convertToFt').addEventListener('click', function (e) {
e.preventDefault(); // Prevent the link from actually navigating

let heightCm = height.value;

if (heightCm) {
let heightFt = (heightCm / 30.48).toFixed(2); // Convert cm to feet
height.value = heightFt; // Set the value to feet
document.querySelector('.inputH label').innerHTML = 'Height (ft)'; // Change label to ft
alert(`Your height is ${heightFt} feet`);
} else {
alert('Please enter your height in cm first.');
}
});

function calculate() {
if (age.value == '' || height.value == '' || weight.value == '' || (male.checked == false && female.checked == false)) {
Expand All @@ -25,9 +38,11 @@ function calculate() {
countBmi();
}
}

function isPositiveNumber(value) {
return /^\d*\.?\d+$/.test(value) && parseFloat(value) > 0;
}

function countBmi() {
var p = [age.value, height.value, weight.value];
if (male.checked) {
Expand Down Expand Up @@ -60,7 +75,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
Loading