const subLength = (word, letter) => {
let count = 0;
// count how many "letter" are in the "word"
for (const item of word){
if (item === letter){
count++;
};
};
// return count;
// only two letters in the word then do calculation.
if (count === 2){
// first letter index
const index1 = word.indexOf(letter);
// second letter index
const index2 = word.indexOf(letter, index1 + 1);
// Distance between two letters(include themselves)
console.log(index2 - index1 + 1);
// return 0 When there are more or less than two letters in a word.
} else {
console.log(0)
};
}
subLength('Saturday', 'a'); // returns 6
subLength('summer', 'm'); // returns 2
subLength('digitize', 'i'); // returns 0
subLength('cheesecake', 'k'); // returns 0
subLength('funny', 'n'); // return 2