Can someone explain solution 2 of missing letters challenge

function fearNotLetter(str) {
  let currCharCode = str.charCodeAt(0);
  let missing = undefined;

  str
    .split("")
    .forEach(letter => {
      if (letter.charCodeAt(0) === currCharCode) {
        currCharCode++;
      } else {
        missing = String.fromCharCode(currCharCode);
      }
    });

  return missing;
}

// test here
fearNotLetter("abce");

Hello there.

Do you have a question?

If so, please edit your post to include it.

Learning to describe problems is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Ok @JeremyLT . I often do that but this time i forgot it :sweat_smile:

If you can tell us more about what you’re confused with in the code you included, we can try to answer your questions.

1 Like

Hi @FrankY4ReaL !

I have edited your post to include the challenge link.

I have also edited your post to mention that you need help understanding solution 2 of this challenge.

Have you solved this challenge yet on own before looking at the solutions?
If not, I would highly suggest solving it your own way first before looking at the solutions.

If you have already solved it, then can you please explain what you don’t understand about this solution?

Thanks!

2 Likes

Can you please explain me what is happening inside forEach method.
Like what will happen after we increment currCharCode for the first time.And also after first iteration in forEach method will the condition will be letter.charCodeAt(1) ?
@ArielLeslie @jwilkins.oboe.
Also we have same characters inside the array as in the string so as i am thinking this will always return true.I know i am wrong somewhere but i don’t know where.Please help!! :disappointed_relieved:

A forEach is just a fancy way of doing a for loop. This is the same as

for(let i = 0; i < str.length; i++) {
    if (str[i].charCodeAt(0) === currCharCode) {
        currCharCode++;
    } 
    else {
        missing = String.fromCharCode(currCharCode);
    }
}
2 Likes

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