Hello to everybody!

I got stuck with 2th exercise in JavaScript Practice: Arrays, Loops, Objects, Iterators. I wrote a function, it works pretty well on tests, but CodeCademy doesn’t pass my answer.

What can be wrong with my code? Thank you in advance for reply

```
const subLength = (str,char) => {
let pattern = new RegExp(char,'g');
let checker = str.match(pattern).length;
if(checker !== 2){
return 0;
}else{
let arr = str.split('');
let indexAll = arr
.map((elem,i) => elem === char ? i : -1)
.filter(index => index !== -1);
return indexAll.reduce((a,b) => Math.abs(a-b)+1);
}
};
```

SOLVED!

Here is my updated way to solve this task:

```
const subLength = (str,char) => {
let pattern = new RegExp(char,'g');
let reg = str.match(pattern);
if(reg !== null){
let checker = reg.length;
if(checker !== 2){
return 0;
}else{
let arr = str.split('');
let indexAll = arr
.map((elem,i) => elem === char ? i : -1)
.filter(index => index !== -1);
return indexAll.reduce((a,b) => Math.abs(a-b)+1);
}
}else{
return 0;
}
};
```