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?

4 posts were split to a new topic: Why would this code not be accepted?

I am struggling to understand how this bit of the code is ever accessed when the len variable is never changed prior to it. Can anybody explain this as I don’t want to use this code without understanding it.

Thank you in advanced

Is this provided by the ‘Get Solution’ or copied from the forums? You are wise to not use code you don’t understand. That is the first rule of programming for beginners… Never use code you didn’t write yourself; and never use could you couldn’t have written by yourself. If you don’t understand the code then clearly you are not the author.

Just looking at the snippet you’ve shown us, one would have to assume that len was initialized at some point. Can’t say. Seem to remember seeing some code like this a few days ago. Wanted to comment on the use of len as a variable name since it equates with ‘length’ in my mind, and that variable does not represent length, but position index (i suggest a loop).

Suggest work this problem out on your own, using hints given in the exercise, and draw upon your own accumulated knowledge base. Leave the reverse engineering for engineers.

thats true, but for example this code works but the platform say its wrong so i would have to use the code in the "view solution even if i didnt write it

// Write function below const subLength = (paramS, paramC)=>{ let hay = 0 let dist = 2 for(i=0;i<paramS.length;i++){ if(paramC===paramS[i]){ hay++ }else if(hay === 1){ dist++ } }if(hay === 2){ return console.log(dist) }else return console.log(0) } subLength('Saturday', 'a'); // returns 6 subLength('summer', 'm'); // returns 2 subLength('digitize', 'i'); // returns 0 subLength('cheesecake', 'k'); // returns 0

There are multiple possible solutions, although the testing may only revolve around one. Yours appears to work correctly, so if you are satisfied with it, get your check mark with the solution, then paste your solution back in and run it so it gets saved.

1 Like

Since .substr() starts at the specified index and extends for a given number of characters afterwards.

We can still use .substr() like this:

const m = str.indexOf(chr)
const n = str.lastIndexOf(chr)
y = str.substr(m, n - m + 1)

subLength('summer', 'm') exemple :
y = str.substr(2, 3 - 2 + 1) // 'mm'

What happens when m is -1 or n is -1?

1 Like

In that case it won’t work like we want, but since we are sure we have exactly two instances of chr in str then str.indexOf(chr) and str.lastIndexOf(chr) won’t return -1.

They’ll both find the same character if there is only one.

const subLength = function (str, chr) {
  let y = str.split('').filter(x => x === chr)
  if (y.length !== 2) return 0
  const m = str.indexOf(chr)
  const n = str.lastIndexOf(chr)
  y = str.substr(m, n - m + 1)
  return y.length;
}

Okay, that code looks familiar. Now we have full context, and you point is more easily made. Yes, I would go along with either string method.

1 Like

It’s my fault I am bad at explaining :rofl:
Thank you for your patience.

1 Like