0

const letsMatch = (string, char) => {
  const newRegex = new RegExp(char, "gi");
  let result = string.match(newRegex);
  const findNegIndex = result.indexOf(char);
  if (findNegIndex === null) {
    return result = 0;
  } else {
    if (result.length === 2) {
    const findFirstIndex = fiveT.indexOf(char);
    const findSecondIndex = fiveT.indexOf(char, findFirstIndex + 1);
    result = findSecondIndex - findFirstIndex + 2;
    return result;
  } else {
    return (result = 0);
  }
  }
}

console.log(letsMatch('totititiTo', 'r'))

line 4: const findNegIndex = result.indexOf(char); Throws Uncaught TypeError: Cannot read properties of null (reading 'indexOf').

4
  • because result is null, possibly also fiveT Commented Aug 10, 2022 at 15:03
  • 2
    The error means exactly what it says. result is null. What matches do you expect string.match to find in this case and why? Commented Aug 10, 2022 at 15:05
  • 1
    Also: if String.indexOf doesn't find the character it returns -1 not null. Commented Aug 10, 2022 at 15:13
  • That's a poor title for this question. It makes it sound like you are attempting to assign null to a variable and can't do it. Although indirectly this may be the case that you wish to permit string.match to return a null value, your actual problem is that you cannot then unconditionally perform result.indexOf because the result may be null. We must all work under certain conditions -- check our results for errors, etc. Commented Aug 10, 2022 at 15:38

2 Answers 2

1

According to the documentation, String.prototype.match() will return null (not an empty array) when no matches are found.

And no matches are found.

You can default to an empty array when it returns null:

const letsMatch = (string, char) => {
  const newRegex = new RegExp(char, "gi");
  let result = string.match(newRegex) || []; // here
  const findNegIndex = result.indexOf(char);
  if (findNegIndex === null) {
    return result = 0;
  } else {
    if (result.length === 2) {
      const findFirstIndex = fiveT.indexOf(char);
      const findSecondIndex = fiveT.indexOf(char, findFirstIndex + 1);
      result = findSecondIndex - findFirstIndex + 2;
      return result;
    } else {
      return (result = 0);
    }
  }
}

console.log(letsMatch('totititiTo', 'r'))

(As an aside, it's not really clear what this function is meant to accomplish, or what you expect return result = 0 to mean other than return 0. But at the very least the error is because you're assuming string.match will always return an array, and there exists a use case where it does not.)

Sign up to request clarification or add additional context in comments.

1 Comment

The function should search the string for the two occurrences of the character and return the length between them including the 2 characters. If there are less than 2 or more than 2 occurrences of the character the function should return 0. Removing findNegIndex and just returning result back with a null object if no matches are founding worked.
0

Depending on what you're coding in...

Use ?. - it'll return undefined if result doesn't have that property, and the || will shunt it on over to the null const findNegIndex = result?.indexOf(char) || null;

or

const findNegIndex = result ? result.indexOf(char) : null;

I'd also shy away from a variable named "string", for readability's sake - since String.match

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.