JavaScript Practice: Arrays, Loops, Objects, Iterators Practice intermediate JavaScript concepts with these 3 code challenges

in this function n° 2 which hire : 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.

among the examples noticed, i have confuse in the example n° 2 :
subLength(‘summer’, ‘m’); // returns 2 .here i think the function sould return 1 instead of 2 as i understand.

The challenge description mentions that it should return the length between them including the 2 characters. The other example output illustrates that too:

subLength('Saturday', 'a'); // returns 6 (instead of 5)

So this means 1 is the only number that the function would never return. It’s either 0, 2, or a higher number

and it’s also more than 2 why it return 6 so as i understand the exercice it should return 6 - 1 =5

The reason is because the challenge wants you to include both characters. Think of it as getting the length of the distance plus the 2 characters. Or act like the rest of the sting doesn’t exist and you’re getting the length of what remains, if that helps you envision it

subLength('summer', 'm'); 

su mm er
   ^^
   2

subLength('Saturday', 'a');

S aturda y
  ^    ^
    6
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.