I’m working on the intermediate JavaScript code challenges. Typically, if I’m having difficulty with a code and look up the solutions, I can figure out what it’s doing and how I can apply it in the future.

I am having trouble understanding the last part of the function for the subLength challenge, which goes as follows:

“Write a function `subLength()`

that takes 2 parameters, a string and a single character. 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.”

The full solution given is:

```
const subLength = (str, char) => {
let charCount = 0;
let len = -1;
for (let i=0; i<str.length; i++) {
if (str[i] == char) {
charCount++;
if (charCount > 2) {
return 0;
}
if (len == -1) {
len = i;
} else {
len = i - len + 1
}
}
}
if (charCount < 2) {
return 0;
}
return len;
};
```

I don’t understand what is happening with this part:

```
}
if (len == -1) {
len = i;
} else {
len = i - len + 1
}
```

I would love to be able to understand the logic of what is going on in this piece of the code.

Thank you in advance for any help!