Why my solution is not working in all cases

ok so here I have created my solution and as it seems It is not working for all tests I do not know why but can anyone here guide me on how can I solve this problem and explain to me why my solution did not work.


function fearNotLetter(str) {
// creating an array of letters to check the sequence 
let letters = [..."abcdefghijklmnopqrstuvwxyz"];

// let's identify where should we start in the letters array
let index = letters.indexOf(str[0]);

// let's create an identifier to loop over the str to loop over the str string and compare the sequence of it with the english letters
let strIndex = 0;

// next step is to create a loop and to identify where we should start in the letters array and start comparing values so we should compare the value one by one with the str
for(let i = index; i < str.length; ++i){ 

// Now the time is to compare sequence i variable is where we start at the letters array and comparing the sequence and when we find a missing letter we return it 
if(str[strIndex] === letters[i]){
  console.log("here we catch the identical values", letters[i])
} else{
  console.log("here we catch the missing letters", letters[i])
  return letters[i]
}

strIndex++

}
// if there is no missing letters return undefined 
return undefined;
}

fearNotLetter("abce");
fearNotLetter("abcdefghjklmno");
fearNotLetter("stvwx");
fearNotLetter("bcdf");
fearNotLetter("abcdefghijklmnopqrstuvwxyz");

Link to the challenge:

The first step for me is always, always, always to look at which tests are passing and which tests are failing. Is there a pattern? What do the tests that are failing have in common? Or in this case, maybe it would be easier to see what the passing tests have in common.

Then start logging things out. See what is happening. It doesn’t matter what you plan is, it matters what is actually happening. No plan survives contact with the enemy. Log some stuff out:

function fearNotLetter(str) {
  let letters = [..."abcdefghijklmnopqrstuvwxyz"];
  let index = letters.indexOf(str[0]);
  let strIndex = 0;
  console.log('index=' + index, 'and str.length=' + str.length)

  for(let i = index; i < str.length; ++i){
    console.log('\n***')
    console.log(`i=${i} strIndex=${strIndex}`)
    console.log('comparing', str[strIndex], 'and', letters[i])
    if(str[strIndex] === letters[i]){
      console.log("here we catch the identical values", letters[i])
    } else{
      console.log("here we catch the missing letters", letters[i])
      return letters[i]
    }
    strIndex++
  }
  return undefined;
}

console.log('\n\nresult is', fearNotLetter("stvwx"));

See what that yields.

In order to be a good programmer, you have to be a good debugger. And being a good debugger means being a good detective.

ok so in order to be a good detective let me detect and ask about what is “\n***”

It just puts in a a dividing line so I can track the different iterations. The \n just puts in a new line and the “ " just adds a " ” line to divide. It’s just to make it easier to read visually. You can get rid of it if you want.

A good detective would have just messed around with it to figure that out. :wink:

1 Like

ok I am trying to understand your code line by line and I am testing it but the console.logs inside the loop is not showing why is that happening ?

Right… that is a symptom of the problem. Look at those values and how your for loop is defined, look at its logic. Keep digging, the answer is there.

as it seems may be there most be a problem that I am not aware of and I cannot figure it out I walk through it letter by letter over and over but I cannot figure the problem can you please tell what is the problem in the loop logic that I have wrote

Your for loop checks its condition before it does its first iteration. What are those two values in the condition at the beginning. I put in a log statement to show you.

Additionally, consider which of the tests are failing. How are they different than the ones that are passing?

Finally, it worked for me.
there was that mistake in my loop condition which make not dynamic and only works for specific tests

Kevin, I am so thankful for your help and guidance next time I will face a scenario like this it will be easier for me to deal with it

1 Like

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