Function subLength()

thanks I got it now .

could someone help me shed some light on why the following code didn’t work? thank you

const subLength = (strg, char) => {
  let count;
  let boolean;

    for (let i = 0; i < strg.length; i++){
      if(strg.charAt(i) === char && count < 2) {
        boolean = true;
      } else {
        boolean = false;
      } 
      while (boolean){
        count++;
      }
    }

When boolean is true, this will be an infinite loop.

so the condition that breaks out of the while loop has to be defined within the while loop?

In this case, yes. Something in the change of count should trigger the break.

1 Like

My code looks like it should work (at least to me), but it doesn’t work (it only returns zero). I’m just curious if the logic I was going for is in the right direction or if I was way off. Thanks!

const subLength = (str, char) => {
let indices = ;
for(let i = 0; i > str.length; i++){
if(str[i] === char){
indices.push(i);
}
};

if(indices.length === 2){
return (indices[1] - indices[0]) + 1;
} else {
return 0;
}
};

Are we being given a character to use as endpoints in a new segment?