subLength solution with substr -- why is it sometimes giving me an extra character?

I just finished the second coding challenge from this page.

I’ve got three subLength()'s that all yield the correct output for the test cases, but only the top version passed the test. (code below)

  1. Why is that?
  2. Why (in subLength1() and subLength2()) does the .substr() method return ‘mme’ instead of ‘mm’?
  3. Have you got any other advice on how to improve my code?

Thanks for your insight!

// Write function below function subLength(word, letter){ let letterCount = word.toLowerCase().split('').filter(x => { return x === letter }).length if(letterCount !== 2){ return 0; } let first = word.toLowerCase().indexOf(letter); word = word.slice(first); let second = word.toLowerCase().indexOf(letter,1) + 1; word = word.slice(0,second); //console.log(first); //console.log(word); return word.length; } function subLength1(word, letter){ let firstOccurence = word.indexOf(letter); let secondOccurence = word.indexOf( letter, firstOccurence + 1 ) let thirdOccurence = word.indexOf( letter, secondOccurence + 1 ) if(firstOccurence === -1 || secondOccurence === -1 || thirdOccurence !== -1){ return 0; } word = word.substr( firstOccurence, secondOccurence ) console.log(word) return (1 + secondOccurence - firstOccurence); } function countLetters(word, letter){ let letterCount = 0; for(var w of word){ if (w === letter){ letterCount++; } } return letterCount; } function subLength2(word, letter){ let letterCount = countLetters(word, letter); if (letterCount !== 2) { return 0 }; first = word.indexOf(letter) second = word.indexOf( letter, first + 1 ) word = word.substr( first, second ); console.log(word); return (1 + second - first); } console.log('subLength()'); console.log(subLength('Saturday', 'a')); // returns 6 console.log(subLength('summer', 'm')); // returns 2 console.log(subLength('digitize', 'i')); // returns 0 console.log(subLength('cheesecake', 'k')) console.log('subLength1()'); console.log(subLength1('Saturday', 'a')); // returns 6 console.log(subLength1('summer', 'm')); // returns 2 console.log(subLength1('digitize', 'i')); // returns 0 console.log(subLength1('cheesecake', 'k')) console.log('subLength2()'); console.log(subLength2('Saturday', 'a')); // returns 6 console.log(subLength2('summer', 'm')); // returns 2 console.log(subLength2('digitize', 'i')); // returns 0 console.log(subLength2('cheesecake', 'k'))