Code solution for Practice JavaScript Syntax exercise subLength()

Hi everyone,

I’ve just finished the second exercise here and I wanted to offer my code as an example for those who may be curious. I think it’s compact and uses the simplest possible logic to accomplish what’s needed.

// Write function below const subLength = (string, letter) => { //converts args to lowercase string = string.toLowerCase() letter = letter.toLowerCase() //indexOf letter in string, tests for multiple instances, saves results to variables let firstLetter = string.indexOf(letter) let secondLetter = string.indexOf(letter, firstLetter + 1) let thirdLetter = string.indexOf(letter, secondLetter + 1) //tests values to see if we should return zero or test distance between letters if (firstLetter === -1 || secondLetter === -1) { return 0 } else if (thirdLetter >= 0) { return 0 } else { return secondLetter - firstLetter + 1 } } console.log(subLength('Saturday', 'a')) //expected 6 console.log(subLength('summer', 'm')) //expected 2 console.log(subLength('digitize', 'i')) //expected 0 console.log(subLength('cheesecake', 'k')) //expected 0

All the tests come out correctly and it was considered valid by the site. Any thoughts on possible improvements or alternate code are welcome. :slightly_smiling_face: