I’d like to start by saying that no solution is a dumb one when you’re still learning. Even in production projects you still see inefficient code because when working to deadlines, working is more important than quick in the first instance. I think your solution is nice, makes good use of loops and string methods to accomplish what needs to be accomplished.
However if you want to see another way of doing it I can give you mine. I like this solution because it’s short, easy to read (in my opinion) and pretty clean. Here it is below.
const subLength = (string, letter) => {
// Escape clause if not 2 instances
if (string.split(letter).length - 1 != 2) return 0
// Find first instance
let position1 = string.indexOf(letter);
// Find second instance
let position2 = string.indexOf(letter, position1+1) + 1
return position2 - position1
}
console.log(subLength('springs', 's')); //output is 7
console.log(subLength('funny', 'n')); //output is 2
console.log(subLength('assets', 's')); //output is 0
console.log(subLength('frank', 'f')); //output is 0
What this does is first checks if the string has exactly 2 instances of the desired letter. It does this by splitting the string into an array on that letter, which will always give you an array with n+1
elements, where n
is the number of instances of the divider. Therefore taking the length of that array-1 will give us n
, and if this is not equal to 2 then the function instantly returns 0, meaning there’s no other code run if the conditions aren’t met. These types of escape clauses are personal preference, but I really like them as you have a clean, clear way to remove all undesired inputs, meaning that the function body is only run in the use case you want.
The next main part is getting position2
as position1
is clear in it’s function. the indexOf()
string method can take a second argument that tells it where in the string to start searching from, in this case we choose the element after the first instance. In this case since there’s only ever 2 instances of a letter this is functionally equivalent to lastIndexOf()
however if more than 2 instances were allowed this method could be the only one that works if you wanted the length between the first and the second instances. Then since strings are zero-indexed you need to add an extra 1 at the end to account for the inclusion of the last character.
Then it’s just simply a case of subtracting position1
from position2
, as position1
’s index is already inclusive of itself, and this gives us our final answers desired as shown at the end of this post.
The last thing to mention is your feeling that your code is complicated for a simple task. The answer is surprisingly that actually working with strings can be far from simple. “Just find the length between two letters” is easy on paper but can be a little complex in code just because of the rigid rules required to actually make the function work for all types of string. However I think it’s a good impulse to always be thinking there could be better ways to write your code, and it’ll lead to you having a strong understanding of all the concepts in the language, since you’ll need to to identify weaknesses in your code.
const subLength = (string, letter) => {
// Escape clause if not 2 instances
if (string.split(letter).length - 1 != 2) return 0
// Find first instance
let position1 = string.indexOf(letter);
// Find second instance
let position2 = string.indexOf(letter, position1+1) + 1
return position2 - position1
}
console.log(subLength('springs', 's')); //output is 7
console.log(subLength('funny', 'n')); //output is 2
console.log(subLength('assets', 's')); //output is 0
console.log(subLength('frank', 'f')); //output is 0