Learn Form Validation by Building a Calorie Counter - Step 63

Tell us what’s happening:

This is not working
alert(Invalid Input: ${invalidInputMatch})

Your code so far

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

/* file: styles.css */

/* 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}`)
    }
  }
}

// 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/123.0.0.0 Safari/537.36

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 63

The lesson wants to display the first element of invalidInputMatch in this alert. In the isInvalidInput function, a string value is returned using the .match() function, which returns an array if the string’s characters match the regex expression.

function isInvalidInput(str) {
  const regex = /\d+e\d+/i;
  return str.match(regex);
}

As a result, currVal will assign an array to the invalidInputMatch variable, and the lesson wants to show this followed by the first value in the invalidInputMatch array in the alert. You can access elements of an array using index numbers. If you include the index of first element there, the problem will be solved. I hope this explanation is clear and helpful to you. Happy coding! :slightly_smiling_face:

7 Likes

I tried this code and it worked.

alert(Invalid Input: ${invalidInputMatch[0]});

1 Like