Javascript formula script off / based off dropdown menu values

Hi there,

I am trying to create a form to calculate a nutrition plan. This is the last piece of the jigsaw.

The form works of the Weight entered in lbs.

Then multiplies this value by x 10 kcal to give us the “totalcalories” field

Total calories value is then used to give us

“CaloriesProtein” totalcalories x 0.40 = ProteinCalories
This is then divided by 4 to gives us “GramsProtein”

CaloriesCarbs is calculated by totalcalories x 0.25 = CarbCalories
this is then divided by 4 to give us “GramsCarbs”

CaloriesFats is calculated by totalcalories x 0.35 = FatsCalories
this is then divided by 9 to give us "gramsFats

So here is where I need help. the Grams of food is to be divided over whatever is selected in the dropdown menu - “No.ofMeals” - values being 3,4 or 5 only.

If “No.ofMeals” selected is 5 I need the “GramsCarbohydrates” to divide by 2 and show in the last two boxes - meal 5 and meal 4 boxes.

If “No.ofMeals” selected is 4 I need the “GramsCarbohydrates” to divide by 2 and show in the boxes meals 4 and meals 3 and not to show at all in Meal 5 box.

If “No.ofMeals” selected is 3 I need the “GramsCarbohydrates” to divide by 2 and show in the boxes Meals 3 and Meals 2 and not to show in Meals 5 or 4 Boxes.

Hope that makes sense.

Thanks,

Chris

Do you have any attempted code?

var numMeals = Number(this.getField("No.ofMeals").valueAsString);
if (numMeals==4 || numMeals==5) event.value = Number(this.getField("GramsCarbohydrates").valueAsString) / 2;
else event.value = ""
const weight = userInput;
const totalCalories = weight * 10;
const proteinCalories = totalCalories * 0.40;
...
<select id="no_of_meals">
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select>

const $numOfMeals = document.querySelector('#num_of_meals');
$numOfMeals.addEventListener('change', handleChangeNumOfMeals);

const handleChangeNumOfMeals = (evt) => {
   let numOfMeals = evt.target.value;
   if (!numOfMeals){
       return;
   }

  const boxMeals = getBoxMeals(numOfMeals);
};

const getBoxMeals = (numOfBoxMeals) => {
   return {
       3:[{ 
           'meal_5': {
              carbs: 12345,
              fats: 111223
           },
          { 
           'meal_4': {
              carbs: 12345,
              fats: 111223
          }
       }],
       4: .....
        
   }[numOfMeals];
};

do you see where I’m going with this?

Hi I’m getting syntax error 9 at line10