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').
resultis null, possibly alsofiveTresultisnull. What matches do you expectstring.matchto find in this case and why?string.matchto return a null value, your actual problem is that you cannot then unconditionally performresult.indexOfbecause the result may be null. We must all work under certain conditions -- check our results for errors, etc.