Learn Form Validation by Building a Calorie Counter - Step 63

Tell us what’s happening: I am stuck where I need to put the code since I put in after the if statement and after the for statement but its just telling me now. That I have an invalid left-hand side.

Your code so far

Use the addition assignment operator to add currVal to your calories total. You’ll need to use the Number constructor to convert currVal to a number.

SyntaxError: unknown: Invalid left-hand side in assignment expression. (47:4)

function getCaloriesFromInputs(list) {
let calories = 0;

for (let i = 0; i < list.length; i++) {
const currVal = cleanInputString(list[i].value);
const invalidInputMatch = isInvalidInput(currVal);

if (invalidInputMatch) {
  alert(`Invalid Input: ${invalidInputMatch[0]}`);
  isError = true;
  return null;
};
 Number(currVal) +=  calories;

}

}
WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

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

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 63

In your expression Number(currVal) += calories;
You are adding like this Number(currVal) = Number(currVal) + calories.
Here you are updating the Number(currVal). However, I think what we asked to do here is to update the calories total, not Number(currVal)

Try writing calories on the LHS and Number(currVal) on the RHS.

2 Likes

Thanks for responding I tried that and still not passing

Can you share the snapshot of your final code?

Also, as far as I can see your function does not return anything.

function getCaloriesFromInputs(list) {
Here you have calculated and updated the calories total
But you have not done anything with the final (calories) value.
}

Once this function getCaloriesFromInputs(list) gets called, calories will be calculated, but the final value will be hanging somewhere.

Can you try adding “return calories” after the “if statement”?

function getCaloriesFromInputs(list) {
let calories = 0;

for (let i = 0; i < list.length; i++) {
const currVal = cleanInputString(list[i].value);
const invalidInputMatch = isInvalidInput(currVal);

if (invalidInputMatch) {
  alert(`Invalid Input: ${invalidInputMatch[0]}`);
  isError = true;
  return null;
};
 calories += Number(currVal);
 return calories;

}

}

function getCaloriesFromInputs(list) {
some code

      for ( ***some code***) {
                  ***some code***
                  if (***....***) {
                           ***some code***
                  };                           // if statement ends here
                   calories += Number(currVal);     //this is the last statement in the for loop
      }; 
      return calories;     // this is the last statement in the function

}

1 Like

Please don’t necro old posts. Thanks