Learn Form Validation by Building a Calorie Counter - Step 65

Tell us what’s happening:

Hi all, please advise what am i doing wrong. It keeps telling me i shouldn’t add an else statement. I think it’s about the placement of the code. I tried so many variations. Is it not after the return statement?
currVal = Number(currVal)+=calories;

Your code so far

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

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

function getCaloriesFromInputs(list) {
  let calories = 0;

  for (const item of list) {
    const currVal = cleanInputString(item.value);
    const invalidInputMatch = isInvalidInput(currVal);

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

    }
  }
}


// User Editable Region
/* file: styles.css */

Your browser information:

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

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 65

let’s talk about syntax errors first:

  1. anything after a return is not executed
  2. You can’t have two assignments on the same line

Now you need to write after the if statement

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.

Thanks for your quick reply!
I am confused about " 1. You can’t have two assignments on the same line", because you mentioned to someone else that it should be done on the same line.
I have tried like this:
‘’‘’‘’‘’‘’‘’
if (invalidInputMatch) {
alert(Invalid Input: ${invalidInputMatch[0]});
isError = true;
currVal=Number(currVal);
calories += currVal;

  return null;
 }

‘’‘’‘’‘’‘’‘’‘’’
and also like this, but it still says: " 2. After your if statement, you should use the addition assignment operator on calories ."
‘’‘’‘’‘’‘’‘’‘’‘’‘’
if (invalidInputMatch) {
alert(Invalid Input: ${invalidInputMatch[0]});
isError = true;
calories += Number(currVal);
return null;
}

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

I am taking your code and adding a comment after the if to show where that is, because it doesn’t look like it is clear to you, after the if is not inside the if.

this is much better, but you need to put it in the right place

1 Like

Thanks! Just figured :slight_smile: