Learn Form Validation by Building a Calorie Counter - Step 84

Tell us what’s happening:

There’s no other answer in the forum concerning this, I tried wrapping remainingCalories in Math.abs() to no avail. Anyone know the reason?

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

function calculateCalories(e) {
  e.preventDefault();
  isError = false;

  const breakfastNumberInputs = document.querySelectorAll("#breakfast input[type='number']");
  const lunchNumberInputs = document.querySelectorAll("#lunch input[type='number']");
  const dinnerNumberInputs = document.querySelectorAll("#dinner input[type='number']");
  const snacksNumberInputs = document.querySelectorAll("#snacks input[type='number']");
  const exerciseNumberInputs = document.querySelectorAll("#exercise input[type='number']");

  const breakfastCalories = getCaloriesFromInputs(breakfastNumberInputs);
  const lunchCalories = getCaloriesFromInputs(lunchNumberInputs);
  const dinnerCalories = getCaloriesFromInputs(dinnerNumberInputs);
  const snacksCalories = getCaloriesFromInputs(snacksNumberInputs);
  const exerciseCalories = getCaloriesFromInputs(exerciseNumberInputs);
  const budgetCalories = getCaloriesFromInputs([budgetNumberInput]);

  if (isError) {
    return;
  }

  const consumedCalories = breakfastCalories + lunchCalories + dinnerCalories + snacksCalories;
  const remainingCalories = budgetCalories - consumedCalories + exerciseCalories;
  const surplusOrDeficit = remainingCalories < 0 ? 'Surplus' : 'Deficit';
  output.innerHTML = `
  <span class="${surplusOrDeficit.toLowerCase()}"> Math.abs(${remainingCalories}) Calorie ${surplusOrDeficit} </span>
  `;
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 84

I would place Math.abs in the {} to see what will happen. you are asked to wrap remainingCalories in the Math.abs() method

That’s a good suggestion. But it didn’t work my good friend. any other ideas brother

Your Math.abs() method need to be wrap within the curly brace.
for example ${Math.abs(variable)}

It seems you’re right. But I typed in this code, which it says it wants, but it still doesn’t go through… Here’s the code:

  <span class="${surplusOrDeficit.toLowerCase()}"> ${Math.abs(remainingCalories)} Calorie ${surplusOrDeficit} </span>
  `;

Its all good! Typed it in with a refreshed page and it worked! Since this is the only question on it, everyone coming after is going to see your brilliance!

1 Like

Glad you did it. Keep coding