Refactor PowerShell script for BMR and macronutrient calculation

- Corrected gender comparison to use `-eq` operator instead of assignment.
- Added input validation for gender to gracefully handle invalid inputs.
- Converted height input from cm to meters for accurate calculation.
- Ensured consistent calculation of BMR and macronutrient splits based on gender.
- Improved output formatting for clarity and readability.
This commit is contained in:
2024-06-24 09:44:30 +00:00
parent a9c3c4c53d
commit b697ca6b29

View File

@@ -1,33 +1,31 @@
#VARS # Prompt for user inputs
[decimal]$Weight = Read-Host "Please input weight in kg" $Weight = Read-Host "Please input weight in kg"
[decimal]$Height = Read-Host "Please input height in cm" $Height = Read-Host "Please input height in cm"
[decimal]$Age = Read-Host "Please input age in years" $Age = Read-Host "Please input age in years"
[char]$Gender = Read-Host "Are you Male (M) or Female (F)".ToUpper() $Gender = Read-Host "Are you Male (M) or Female (F)? (M/F)"
#determining split (female) # Convert height to meters for calculation (assuming input is in cm)
if ($Gender = "F") { $HeightMeters = [decimal]$Height / 100
$Calories = 655.1 + (9.563 * $weight) + (1.850 + $Height) - (4.676 * $Age)
# Determine BMR and macronutrient splits based on gender
if ($Gender -eq "F" -or $Gender -eq "f") {
$Calories = 655.1 + (9.563 * [decimal]$Weight) + (1.850 * [decimal]$Height) - (4.676 * [decimal]$Age)
}
elseif ($Gender -eq "M" -or $Gender -eq "m") {
$Calories = 66.47 + (13.75 * [decimal]$Weight) + (5.003 * [decimal]$Height) - (6.755 * [decimal]$Age)
}
else {
Write-Host "Invalid input for gender. Exiting."
Exit
}
# Calculate macronutrient calories
$Protein_Cals = ($Calories / 10) * 2 $Protein_Cals = ($Calories / 10) * 2
$Carbs_Cals = ($Calories / 10) * 6 $Carbs_Cals = ($Calories / 10) * 6
$Fat_Cals = ($Calories / 10) * 2 $Fat_Cals = ($Calories / 10) * 2
Write-Host "Your BMR is:"([math]::Round($Calories,0))
# Display results
Write-Host "Your BMR is:" ([math]::Round($Calories, 0)) "kcals"
Write-Host "Your recommended protein intake is:" ([math]::Round($Protein_Cals, 0)) "kcals" Write-Host "Your recommended protein intake is:" ([math]::Round($Protein_Cals, 0)) "kcals"
Write-Host "Your recommended carbohydrate intake is:" ([math]::Round($Carbs_Cals, 0)) "kcals" Write-Host "Your recommended carbohydrate intake is:" ([math]::Round($Carbs_Cals, 0)) "kcals"
Write-Host "Your recommended fat intake is:" ([Math]::Round($Fat_Cals, 0)) "kcals" Write-Host "Your recommended fat intake is:" ([Math]::Round($Fat_Cals, 0)) "kcals"
}
#determing split (male)
elseif ($Gender = "M") {
$Calories = 66.47 + (13.75 * $weight) + (5.003 + $Height) - (6.755 * $Age)
$Protein = $Calories/4
$Carbs = $Calories/4
$Fats = $Calories/9
Write-Host "Your BMR is: " $Calories
Write-Host "Your recommended protein intake is: " $Protein
Write-Host "Your recommended carbohydrate intake is: " $Carbs
Write-Host "Your recommended fat intake is: " $Fats
}
else {
Exit-PSSession
}
Exit-PSSession