Missing letters - trouble figuring out solution

Do you have any idea why this doesn’t work ?

function fearNotLetter(str) {
  var final = [];
  for (i = 0; i < str.length; i++) {
    var a = str[i].charCodeAt(0); 
    var b = (str[i].charCodeAt(0)) + 1;
    if (b != a + 1) {
      final.push(String.fromCharCode(a + 1));  
  return final;


For me the program is lopping through the parameter string, putting inside a the char code of str[i] and in b the char code of str[i + 1]. So then if b != a + 1 this should mean there is a missing character and I should add it to my final array right ?

I know I didn’t handle the undefined return and I’m not sure if there are two letter missing in a row he will act the right way but one problem at the time, I would like to get why this doesn’t work

I think the problem comes from

  if (b != a + 1) {
      final.push(String.fromCharCode(a + 1));  

Still block, this is driving me crazy

I moved this out of the other thread, because your question concerns a completely different challenge…

You are currently returning an array. The final result which should be returned is a string (a letter) or undefined.

Have you written out an algorithm before attempting to write code? I suggest writing out the steps you would take if you were doing this manually without a computer. Then transcribe those steps into code.

Post your steps and we can first see if your logic is correct. After that, writing the code is easy.

The reason I have suggested writing your steps down, is because I am not sure what direction you are trying to take with pushing an ascii code into an array if b != a +1. If b != a + 1, what is that telling you?

1 Like

Hey yeah I was saying “maybe instead of poping a new subject again i can bother you one more time” haha

So next time I will open new topic for each different chall sorry

Yeah I know I return an array, I was returning final.join() but I cut the .join() to test.

With join it return nothing, without it return an empty array …

For me when I say
a = ascii code of str[i]
b = ascii code of str[i] + 1

Then if b is not equal to a + 1 that mean that b is not the following character of the alphabet so I must write in my array the following letter which would be fromCharCode of a + 1

The ascii code for ‘a’ is 97 (a in your code). The ascii code for ‘b’ is 98 (b in your code). So, your code is not going to push ‘b’ into the array, because 98 != 97+1 is false, nor ‘c’ because 99 !=98+1 is false, nor ‘d’ because 100 !=99+1 is false, and finally not ‘f’ because 101 != 100+1 is false.

a and b are variables in my code, i should have name them different

I understand that, but the string in the example code you showed is “abce” and I was showing you what your if statement comparison is evaluating during each iteration of the for loop.

1 Like

on the test “abce” it should do :

a = 97 b = 98 c = 99 d = 100 e = 101

b is equal to a + 1
c is equal to b + 1
e is not equal to c + 1

well at least I think because there is not d in the “abce” test
I’m probably wrong because I think my problem lie in the if statement

Sorry I was wondering if you where talking about the letters or the variables so I precise but I was stupid name them like that …

You still have not given me a written algorithm, so I will give you one.

#1) Start at the first character of str.

#2) Is the difference between the ascii code of current character and the ascii code of next character greater than 1? If yes, then return the character of the current character’s ascii code + 1. If no, continue to next step

#3) Is this the last character of string? If yes, return undefined. If no, move to the next character and go back to step 2

It is a simple algorithm and can now be easily transcribed to any programming language.

I understood you ask me if I did write an algorithm before not that you ask me to do it

What’s you’ve wrote is more or less what I was trying to do with if b != a + 1
Because b is the ascii code of str at index + 1
And a is the ascii code of str at index
So if the difference is not 1, there is a missing letter

I think I will start again from nothing and try to find another way

Actually, I did ask you to write out your algorithm.

My bad then … I’ve spent more time than I though on that problem, guess I’m starting to get tired if I don’t read properly :confused:

I think my problem came from the fact that I was comparing i and i + 1 but after the last character of the string, charCodeAt of i + 1 was out of the string an so return an error

FYI - The following to lines of code accomplish the same thing in your original code.

var a = str[i].charCodeAt(0); 


var a = str.charCodeAt(i); 

Also, if you do not pass a value into the charCodeAt function, it assumes 0, so you could write the first line above as:

var a = str[i].charCodeAt(); 

Just wanted to share a couple of different ways you may see in other solutions.

1 Like

If you are still struggling on this challenge, I will provide you the code (blurred out but click to see) for my step #2 logic. This still assumes you use your original code for what you assign to variables a and b. Only click on the blurred part if you want to how to solve 95% of your code’s issues.

if ((b - a) > 1) {
  return String.fromCharCode(a+1);
1 Like

Hey sorry last night I was really tired I went to sleep …

Today at start again, I tried a different approach but still doesn’t work :

 var first = array[i];
 var second = array[i + 1];

And then it go in the if statement while it wasn’t supposed to.
Took me 20 minutes before I though it may come from the fact that array[i + 1] when i = array.length is out of the str
So I’ve tried with i starting at 1, first is array[i - 1] and second just array[i] and … it work

So much time spent on that simple problem … but at least when you get the solution that’s a strange feeling, feel rewarding even if I’ve spent hours to do a very simple stuff

Here is my final answer to this challenge

I’m reading your answers, thanks for the
"FYI - The following to lines of code accomplish the same thing in your original code"
Because it’s typically the stuff I don’t get, I suck at reading documentation, I always confuse myself

What I love to do, instead of documentation is reading examples of code and try to figure out how it work
I mean I should do both because sometimes there are very importants infos in the documentation but it is clearer to me with examples than with documentation.

I always wonder why the guy who wrote documentation that would be read by thousands of people doesn’t make a little pedagogic effort :see_no_evil: