Missing letters (&& condition not working)

Missing letters (&& condition not working)
0

#1

Tell us what’s happening:

First help post, sorry if it isn’t formatted correctly.

I am not sure why I can’t return undefined with this && condition? I found some of the other solutions on the forums but want to understand why this, in particular, doesn’t work.

Otherwise the missing checker is working correctly.

Thanks for the advice!

Your code so far


function fearNotLetter(str) {

  let charCodeArr = [];

  for (let i = 0; i < str.length; i++) {
    charCodeArr.push(str.charCodeAt([i]))
  }

  for(var i = 1; i < charCodeArr.length; i++) {

    if (charCodeArr[i] - charCodeArr[i-1] != 1) {
      let missingNum = charCodeArr[i] - 1;
      return String.fromCharCode(missingNum)
    } 
    else if (charCodeArr[i] - charCodeArr[i-1] == 1 && charCodeArr[i] - charCodeArr[i-1] != 2) {
      return undefined;
    }
}

  return str;
}

fearNotLetter("abce");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters/


#2

What do you mean?

You are returning undefined with && condition.

image


#3

The issue seems to be that I can’t have both conditions work. If the && statement works the other statement doesn’t.


#4

Not understanding your question concerning the line above. Can you explain what you are trying to do in this else if statement? Just to be clear, both of the conditions in the else if statement must be true for undefined to be returned. Why? Because && means AND.


#5

I am using it so that it doesn’t trigger before the loop finds the missing character.

I am essentially trying to make a condition that doesn’t automatically occur before it should with the full alphabet case.

I can tell it won’t work but can’t quite figure out the alternative besides copying someone else’s answer.


#6

What I am trying to get you to explain is what do you think these conditions are actually checking?


#7

I figured it out.

Just my misunderstanding of the scope of returns.


#8

I do realize how I was incorrectly using the && statement.


#9

Thanks for the help!