I have a feature with three sections . The first section is used to caculate BMI and then that BMI is used to give a score to that section this then should be added to the total score . The issue is that whatever the BMI is the total score is always shown as 0 that is not correct.
Here is the relevant code . Any help working out what i have done wrong would be very helpful.
updateBMI(sectionIndex: number, choices: any[]): void {
// Find the choices for height and weight
const heightChoice = choices.find(choice => choice.label === "Height(cm)");
const weightChoice = choices.find(choice => choice.label === "Weight (kg)");
// Check if both height and weight choices are found
if (heightChoice && weightChoice) {
const height = heightChoice.value;
const weight = weightChoice.value;
// Perform BMI calculation
const bmi = this.calculateBMI(height, weight);
// Calculate section score
const sectionScore = this.calculateSection1Score(sectionIndex, bmi);
// Update BMI Score choice in choices array
const bmiScoreChoice = choices.find(choice => choice.label === "BMI Score");
if (bmiScoreChoice) {
bmiScoreChoice.value = Math.round(bmi).toString();
}
// Update the original Sections array with the modified choices
this.Sections[sectionIndex].choices = choices;
// Update the section score property
this.sectionScore = sectionScore;
// Calculate total score
this.calculateTotalScore();
}
}
calculateBMI(height: number, weight: number): number {
// BMI formula: weight (kg) / height (m) ^ 2
const bmi = weight / Math.pow(height / 100, 2); // Convert height from cm to meters
return Math.floor(bmi);
}calculateSection1Score(sectionIndex: number, bmi: number): number {
let sectionScore: number;
if (bmi > 20) {
sectionScore = 0; // Obese
} else if (bmi >= 18.5 && bmi <= 20) {
sectionScore = 1;
} else {
sectionScore = 2;
}
// Recalculate total score whenever the section score changes
this.calculateTotalScore();
return sectionScore;
// Return the calculated section score
}
calculateTotalScore(): void {
let totalScore = 0;
// Calculate total score from sectionScore
if (this.sectionScore !== null) {
totalScore += this.sectionScore;
}
// Calculate total score from score property in section 2
if (this.score !== null) {
totalScore += this.score;
}
// Calculate total score from selectedOptions in section 3
for (const key of Object.keys(this.selectedOptions)) {
console.log(this.selectedOptions[key]);
totalScore += this.selectedOptions[key].score;
}
// Assign the calculated total score to the totalScore property
this.totalScore = totalScore;
}
I have tried updating the updateBMI function but not fixed it.