Skip to content
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
107 changes: 89 additions & 18 deletions BMICalculator/jadhav-kunal/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,99 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<title>BMI Calculator</title>
</head>
<body>
<div class="background"></div>
<div class="container">
<h1>BMI Calculator</h1>
<form>
<p class="units"><label>Units: </label><select type="select" id="units">
<option>Metric</option>
<option>Imperial</option>
</select></p>
<p><label id="height-label">Height in CM: </label><input type="text" name="height" id="height"></p>
<p><label id="weight-label">Weight in KG: </label><input type="text" name="weight" id="weight"></p>
<button type="submit">Calculate</button>
<div id="results"></div>
<div id="weight-guide">
<h3>BMI Weight Guide</h3>
<p>Under Weight = Less than 18.6</p>
<p>Normal Range = 18.6 and 24.9</p>
<p>Overweight = Greater than 24.9</p>
</div>
</form>
<div class="calculator-card">
<div class="header">
<h1>BMI Calculator</h1>
<p class="subtitle">Calculate your Body Mass Index</p>
</div>

<form id="bmi-form">
<div class="form-group">
<label class="form-label">Measurement Units</label>
<div class="unit-toggle">
<button type="button" class="unit-btn active" data-unit="metric">Metric</button>
<button type="button" class="unit-btn" data-unit="imperial">Imperial</button>
</div>
</div>

<div class="input-group">
<label id="height-label" class="input-label">Height (cm)</label>
<div class="input-wrapper">
<input type="number" name="height" id="height" placeholder="Enter height" step="0.1" min="0">
<span class="input-unit" id="height-unit">cm</span>
</div>
</div>

<div class="input-group">
<label id="weight-label" class="input-label">Weight (kg)</label>
<div class="input-wrapper">
<input type="number" name="weight" id="weight" placeholder="Enter weight" step="0.1" min="0">
<span class="input-unit" id="weight-unit">kg</span>
</div>
</div>

<button type="submit" class="calculate-btn">
<span>Calculate BMI</span>
<svg width="20" height="20" viewBox="0 0 20 20" fill="none">
<path d="M7.5 15L12.5 10L7.5 5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
</form>

<div id="results" class="results hidden">
<div class="bmi-value">
<span class="bmi-number" id="bmi-number">0</span>
<span class="bmi-label">BMI</span>
</div>
<div class="bmi-category" id="bmi-category">
<span id="category-text"></span>
</div>
<div class="bmi-range" id="bmi-range"></div>
</div>

<div class="weight-guide">
<h3>BMI Categories</h3>
<div class="guide-items">
<div class="guide-item underweight">
<div class="guide-color"></div>
<div class="guide-text">
<span class="guide-label">Underweight</span>
<span class="guide-range">&lt; 18.5</span>
</div>
</div>
<div class="guide-item normal">
<div class="guide-color"></div>
<div class="guide-text">
<span class="guide-label">Normal</span>
<span class="guide-range">18.5 - 24.9</span>
</div>
</div>
<div class="guide-item overweight">
<div class="guide-color"></div>
<div class="guide-text">
<span class="guide-label">Overweight</span>
<span class="guide-range">25 - 29.9</span>
</div>
</div>
<div class="guide-item obese">
<div class="guide-color"></div>
<div class="guide-text">
<span class="guide-label">Obese</span>
<span class="guide-range">≥ 30</span>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
<script src="script.js"></script>
</html>
263 changes: 218 additions & 45 deletions BMICalculator/jadhav-kunal/script.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,232 @@
const form = document.querySelector('form');
const units = document.querySelector('#units');
const form = document.querySelector('#bmi-form');
const unitButtons = document.querySelectorAll('.unit-btn');
const heightLabel = document.querySelector('#height-label');
const weightLabel = document.querySelector('#weight-label');
const height = document.querySelector('#height');
const weight = document.querySelector('#weight');
const heightInput = document.querySelector('#height');
const weightInput = document.querySelector('#weight');
const heightUnit = document.querySelector('#height-unit');
const weightUnit = document.querySelector('#weight-unit');
const resultsDiv = document.querySelector('#results');
const bmiNumber = document.querySelector('#bmi-number');
const categoryText = document.querySelector('#category-text');
const bmiRange = document.querySelector('#bmi-range');

let currentUnit = 'metric';

form.addEventListener('submit', function(e){
e.preventDefault();
const formData = new FormData(e.target);
const inputValues = Object.fromEntries(formData);
let height = +inputValues.height;
let weight = +inputValues.weight;
const results = document.querySelector('#results');
if(units.value === "Imperial") {
height = height * 2.54;
weight = weight * 0.453592;
console.log(units.value)
}
if((height === '') || (height < 0) || (isNaN(height))){
//NaN !== NaN
results.innerHTML = "Please provide a valid height";
// Unit toggle functionality
unitButtons.forEach(btn => {
btn.addEventListener('click', function() {
// Remove active class from all buttons
unitButtons.forEach(b => b.classList.remove('active'));
// Add active class to clicked button
this.classList.add('active');

currentUnit = this.dataset.unit;
updateLabels();

} else if (weight === '' || weight < 0 || isNaN(weight)){
results.innerHTML = "Please provide a valid weight";
// Convert values if they exist
if (heightInput.value || weightInput.value) {
convertValues();
}
});
});

function updateLabels() {
if (currentUnit === 'imperial') {
heightLabel.textContent = 'Height (in)';
weightLabel.textContent = 'Weight (lb)';
heightUnit.textContent = 'in';
weightUnit.textContent = 'lb';
} else {
//calculate BMI
const bmi = (weight / ((height*height)/10000)).toFixed(2);
//display the results
results.innerHTML = `<span>${bmi}</span>`
}
heightLabel.textContent = 'Height (cm)';
weightLabel.textContent = 'Weight (kg)';
heightUnit.textContent = 'cm';
weightUnit.textContent = 'kg';
}
}

function convertValues() {
if (currentUnit === 'imperial') {
// Convert from metric to imperial
if (heightInput.value) {
heightInput.value = (parseFloat(heightInput.value) / 2.54).toFixed(2);
}
if (weightInput.value) {
weightInput.value = (parseFloat(weightInput.value) / 0.453592).toFixed(2);
}
} else {
// Convert from imperial to metric
if (heightInput.value) {
heightInput.value = (parseFloat(heightInput.value) * 2.54).toFixed(2);
}
if (weightInput.value) {
weightInput.value = (parseFloat(weightInput.value) * 0.453592).toFixed(2);
}
}
}

function getBMICategory(bmi) {
if (bmi < 18.5) {
return {
category: 'underweight',
text: 'Underweight',
range: 'Below 18.5'
};
} else if (bmi >= 18.5 && bmi < 25) {
return {
category: 'normal',
text: 'Normal Weight',
range: '18.5 - 24.9'
};
} else if (bmi >= 25 && bmi < 30) {
return {
category: 'overweight',
text: 'Overweight',
range: '25 - 29.9'
};
} else {
return {
category: 'obese',
text: 'Obese',
range: '30 and above'
};
}
}

function displayResults(bmi) {
const categoryInfo = getBMICategory(bmi);

// Remove all category classes
resultsDiv.classList.remove('underweight', 'normal', 'overweight', 'obese');
// Add the appropriate category class
resultsDiv.classList.add(categoryInfo.category);

// Animate BMI number
animateValue(bmiNumber, 0, bmi, 1000);

// Update category text and range
categoryText.textContent = categoryInfo.text;
bmiRange.textContent = `BMI Range: ${categoryInfo.range}`;

// Show results
resultsDiv.classList.remove('hidden');

// Scroll to results smoothly
resultsDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}

function animateValue(element, start, end, duration) {
let startTimestamp = null;
const step = (timestamp) => {
if (!startTimestamp) startTimestamp = timestamp;
const progress = Math.min((timestamp - startTimestamp) / duration, 1);
const current = progress * (end - start) + start;
element.textContent = current.toFixed(1);
if (progress < 1) {
window.requestAnimationFrame(step);
} else {
element.textContent = end.toFixed(1);
}
};
window.requestAnimationFrame(step);
}

form.addEventListener('submit', function(e) {
e.preventDefault();

let height = parseFloat(heightInput.value);
let weight = parseFloat(weightInput.value);

// Validation
if (!height || height <= 0 || isNaN(height)) {
showError('Please enter a valid height');
heightInput.focus();
return;
}

if (!weight || weight <= 0 || isNaN(weight)) {
showError('Please enter a valid weight');
weightInput.focus();
return;
}

// Convert to metric if imperial
if (currentUnit === 'imperial') {
height = height * 2.54; // inches to cm
weight = weight * 0.453592; // pounds to kg
}

// Validate converted values
if (height < 50 || height > 300) {
showError('Height should be between 50cm and 300cm (or equivalent)');
return;
}

if (weight < 10 || weight > 500) {
showError('Weight should be between 10kg and 500kg (or equivalent)');
return;
}

// Calculate BMI: weight (kg) / height (m)²
// Height is in cm, so we divide by 100 to get meters
const heightInMeters = height / 100;
const bmi = weight / (heightInMeters * heightInMeters);

// Display results
displayResults(bmi);
});

function showError(message) {
// Remove any existing error styling
heightInput.style.borderColor = '';
weightInput.style.borderColor = '';

// Show error message (you could add a toast notification here)
alert(message);

// Add error styling to inputs
if (!heightInput.value || heightInput.value <= 0) {
heightInput.style.borderColor = '#ff6b6b';
}
if (!weightInput.value || weightInput.value <= 0) {
weightInput.style.borderColor = '#ff6b6b';
}

// Remove error styling after 3 seconds
setTimeout(() => {
heightInput.style.borderColor = '';
weightInput.style.borderColor = '';
}, 3000);
}

// Allow user to change between metric and imperial.
units.addEventListener('change', (e) => {
if(e.target.value === 'Imperial') {
heightLabel.textContent = "Height in IN: "
weightLabel.textContent = "Weight in LB: "
// Add input validation on blur
heightInput.addEventListener('blur', function() {
const value = parseFloat(this.value);
if (this.value && (value <= 0 || isNaN(value))) {
this.style.borderColor = '#ff6b6b';
} else {
this.style.borderColor = '';
}
});

if(height.value){
// convert current input to imperial
height.value = (+height.value / 2.54).toFixed(2);
weight.value = (+weight.value / 0.453592).toFixed(2);
weightInput.addEventListener('blur', function() {
const value = parseFloat(this.value);
if (this.value && (value <= 0 || isNaN(value))) {
this.style.borderColor = '#ff6b6b';
} else {
this.style.borderColor = '';
}
} else {
heightLabel.textContent = "Height in CM: "
weightLabel.textContent = "Weight in KG: "
});

if(height.value){
// convert current input to metric
height.value = (+height.value * 2.54).toFixed(2);
weight.value = (+weight.value * 0.453592).toFixed(2);
// Allow Enter key to submit
heightInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
weightInput.focus();
}
}

});

} )
weightInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
form.dispatchEvent(new Event('submit'));
}
});
Loading