Hi folks, here’s the link to the exercise I’m stuck on. For the second Code Challenge, can someone please help me figure out why this solution isn’t considered valid? It passes every test I’ve tried. Looking at the solution, I don’t understand why initializing a variable len
to -1 would be necessary.
const subLength = (str, char) => {
let counts = [];
for (i = 0; i < str.length; i++){
if (str[i] === char) {
counts.push(i);
}
}
if (counts.length != 2) {
return 0;
} else {
return counts[1] - counts[0] + 1;
}
}