JavaScript Practice: Arrays, Loops, Objects, Iterators

I have been working on this practice.

This was the task:
Write a function subLength() that takes 2 parameters, a string and a single character. The function should search the string for the two occurrences of the character and return the length between them including the 2 characters. If there are less than 2 or more than 2 occurrences of the character the function should return 0.

I had a hard time trying to code this out. When I checked the solution code, this is what I got:
const subLength = (str, char) => {
let charCount = 0;
let len = -1;

for (let i=0; i<str.length; i++) {
if (str[i] == char) {
charCount++;
if (charCount > 2) {
return 0;
}
if (len == -1) {
len = i;
} else {
len = i - len + 1
}
}
}
if (charCount < 2) {
return 0;
}

return len;
};

The solution does work but I still don’t understand it nor understand how it could’ve fulfilled the given task. Can somebody explain this?

Hello!
The first three lines just set the function up and define the two variables which will be used: len for the length of the substring and charCount for checking the number of time a specific character appears in the original string.


for (let i=0; i<str.length; i++) {

Creates a for loop to iterate through the original string.

This checks to see if the item at the current list index is the specified character. If it is, it increases the charCount variable (used to keep track of the characters) by 1,


if (charCount > 2) {
return 0;
}

Here, it checks to see if the charCount variable is greater than 2. If it is, it returns 0 (like specified in the instructions).


if (len == -1) {
len = i;

This is the first part of the code involved in actually finding the length between the two characters. If the length is -1 (something which could never happen if you were working through a list in the fashion this code is, and therefore why len was initialised with -1 as its value at the start), then len gets set to -1.
This will only happen the first time the loop encounters the specific character char.


Here, the number of characters between the last time len was set and now is found by subtracting the current value of i (the current position in the for loop) and the value of the index char was last found. One is added to avoid off by one errors.


This is simply a fail-safe, in case there is less than two examples of the character char in the original string.


This returns the distance between the two characters if there are exactly two of the specified characters in the original string.

Something to keep in mind

All of this code:

if (str[i] == char) {
charCount++;
if (charCount > 2) {
return 0;
}
if (len == -1) {
len = i;
} else {
len = i - len + 1
}
}

is in the for loop, so it executes in every iteration of the for loop; it executes for every character in the input string.

I hope this helps!

Hi, thanks a lot for this. I really appreciate it. But one thing I am struggling to find out is how does the distance between the current position in the for loop and the set length len find the distance between the characters?

The distance between the characters is also the number of characters between them. That means that if you find the current position of the loop in the string, and subtract it from the position of last character char and add one, then you’ll find the number of characters between them:

"some stri"
 012345678//these are the characters of the string, numbered.
//consider the character "s". Where is the first example?
//At position "0". Where is the second example? At position 5.
//Now, subtract zero from 5: 5-0 = 5. Add 1 to make sure you
//include both the starting and ending character. 5+1=6.
//So there's six characters between the two "s" characters, including
//both letters "s". Now count them.