-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #391 from SubhaBhatta/newFeature
New features added.
- Loading branch information
Showing
3 changed files
with
130 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,60 @@ | ||
document.getElementById("submit").addEventListener("click", bmiCalculator); | ||
const unitSelect = document.getElementById('unit'); | ||
const heightInput = document.getElementById('height'); | ||
const weightInput = document.getElementById('weight'); | ||
const heightUnit = document.getElementById('height-unit'); | ||
const weightUnit = document.getElementById('weight-unit'); | ||
const calculateBtn = document.getElementById('calculate'); | ||
const resetBtn = document.getElementById('reset'); | ||
const resultDiv = document.getElementById('result'); | ||
|
||
function bmiCalculator() { | ||
var cm = parseInt(document.getElementById("cm").value); | ||
var kg = parseFloat(document.getElementById("kg").value); | ||
unitSelect.addEventListener('change', updateUnits); | ||
calculateBtn.addEventListener('click', calculateBMI); | ||
resetBtn.addEventListener('click', resetCalculator); | ||
|
||
var bmi; | ||
var newCm= parseFloat(cm/100); | ||
function updateUnits() { | ||
if (unitSelect.value === 'metric') { | ||
heightUnit.textContent = 'cm'; | ||
weightUnit.textContent = 'kg'; | ||
} else { | ||
heightUnit.textContent = 'in'; | ||
weightUnit.textContent = 'lbs'; | ||
} | ||
} | ||
|
||
bmi = kg / (newCm * newCm); | ||
bmi = bmi.toFixed(1); | ||
// console.log(bmi); | ||
function calculateBMI() { | ||
const height = parseFloat(heightInput.value); | ||
const weight = parseFloat(weightInput.value); | ||
|
||
document.getElementById("result").innerHTML = "Your BMI is " + bmi; | ||
} | ||
if (isNaN(height) || isNaN(weight) || height <= 0 || weight <= 0) { | ||
resultDiv.innerHTML = '<p class="error">Please enter valid height and weight values.</p>'; | ||
return; | ||
} | ||
|
||
let bmi; | ||
if (unitSelect.value === 'metric') { | ||
bmi = weight / ((height / 100) ** 2); | ||
} else { | ||
bmi = (weight / (height ** 2)) * 703; | ||
} | ||
|
||
const category = getBMICategory(bmi); | ||
resultDiv.innerHTML = ` | ||
<p>Your BMI is ${bmi.toFixed(1)}</p> | ||
<p>Category: ${category}</p> | ||
`; | ||
} | ||
|
||
function getBMICategory(bmi) { | ||
if (bmi < 18.5) return 'Underweight'; | ||
if (bmi < 25) return 'Normal weight'; | ||
if (bmi < 30) return 'Overweight'; | ||
return 'Obesity'; | ||
} | ||
|
||
function resetCalculator() { | ||
heightInput.value = ''; | ||
weightInput.value = ''; | ||
resultDiv.innerHTML = ''; | ||
unitSelect.value = 'metric'; | ||
updateUnits(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,67 @@ | ||
* { | ||
padding: 0; | ||
margin: 0; | ||
font-family: 'Lato', Arial, sans-serif; | ||
font-size: 100%; | ||
line-height: 1.25; | ||
color: #FFF; | ||
font-family: 'Arial', sans-serif; | ||
box-sizing: border-box; | ||
} | ||
|
||
body { | ||
background: #8e44e2; | ||
color: rgba(0, 0, 0, 0.45); | ||
color: #FFF; | ||
line-height: 1.6; | ||
padding: 20px; | ||
} | ||
|
||
.container { | ||
margin: 1% auto 0; | ||
width: 65%; | ||
} | ||
|
||
@media (max-width: 600px) { | ||
.container { | ||
margin: 5% auto 0; | ||
width: 75%; | ||
} | ||
max-width: 600px; | ||
margin: 0 auto; | ||
background: rgba(255, 255, 255, 0.1); | ||
padding: 20px; | ||
border-radius: 8px; | ||
} | ||
|
||
h1 { | ||
width: 100%; | ||
font-size: 2em; | ||
h1, h2 { | ||
text-align: center; | ||
margin: 2% auto; | ||
} | ||
|
||
.info { | ||
background: rgba(255, 255, 255, .09); | ||
padding: 2%; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.result { | ||
font-size: 35px; | ||
text-align: center; | ||
.input-group { | ||
margin-bottom: 15px; | ||
} | ||
|
||
label { | ||
margin: 3% 0 1%; | ||
position: relative; | ||
float: left; | ||
} | ||
|
||
input, textarea, select { | ||
line-height: 1.5; | ||
font-size: 1.4em; | ||
padding: 5px 10px; | ||
border: 2px solid #FFF; | ||
color: #fff; | ||
display: block; | ||
width: 100%; | ||
background: transparent; | ||
box-sizing: border-box; | ||
outline: 0; | ||
margin-bottom: 5px; | ||
} | ||
|
||
.display-input { | ||
input[type="number"], select { | ||
width: 100%; | ||
height: 100px; | ||
text-align: center; | ||
background-color: #1f80c9; | ||
padding: 8px; | ||
border: 1px solid #ddd; | ||
border-radius: 4px; | ||
background: rgba(255, 255, 255, 0.2); | ||
color: #FFF; | ||
} | ||
|
||
.submit { | ||
width: 150px; | ||
border-radius: 5px; | ||
border-style: none; | ||
background: #8e44e2; | ||
button { | ||
display: inline-block; | ||
background: #4CAF50; | ||
color: #FFF; | ||
font-size: 25px; | ||
margin: 5% auto; | ||
border: 2px solid #FFF; | ||
padding: 10px 20px; | ||
border: none; | ||
border-radius: 4px; | ||
cursor: pointer; | ||
transition: all .6s; | ||
margin-right: 10px; | ||
} | ||
button:hover { | ||
background: #45a049; | ||
} | ||
.result { | ||
margin-top: 20px; | ||
padding: 15px; | ||
background: rgba(0, 0, 0, 0.2); | ||
border-radius: 4px; | ||
} | ||
.info { | ||
margin-top: 20px; | ||
background: rgba(255, 255, 255, 0.1); | ||
padding: 15px; | ||
border-radius: 4px; | ||
} | ||
|
||
.submit:hover { | ||
background: #FFF; | ||
color: #8e44e2; | ||
.error { | ||
color: #FF6B6B; | ||
margin-top: 5px; | ||
} |