I have a Javascript iteration/Regex expression that's been driving me insane and was hoping someone could provide some insight, as the challenge I'm running into seems to be something peculiar to JS. As you can see, the regex expression I have checks to see if the letter occurs 2 or more times in the string and if it doesn't then returns the letter at the specified index. However, for a string such as "testing" it will return "t" despite "t" existing twice in the string. Yet, other strings, such as "aaaaac" it will correctly return "c". I've tested it in Rubular and the Regex expression works fine, so I haven't been able to determine why it won't work in this context.
function found (str) {
for (const number in str) {
let regex = new RegExp( str[number] + "{2,}");
if (!str.match(regex)) {
return str[number]
}
}
}
I would greatly appreciate any insight that could be offered!