Missing letters return string?

Tell us what’s happening:
Hey :slight_smile:

code seems to be working, at least console shows the correct letter to return…

Problem is my code won’t pass the tests.

What could be the problem?

Your code so far


function fearNotLetter(str) {

let a="";

for(let x=0;x<str.length-1;x++){

  if( str.charCodeAt(x+1)!=str.charCodeAt(x)+1 ){
    let a=String.fromCharCode(str.charCodeAt(x)+1);
    console.log(a)    
  }else a=undefined;
}

  return a;
}

fearNotLetter("bcdf");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36.

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

Your issue is this

Add console.log(a) right above the return statement

And check your code with this tool
http://pythontutor.com/javascript.html

You really need small tweaks, almost there

1 Like

got it!


function fearNotLetter(str) {
let a="";
for(let x=0;x<str.length-1;x++){

  if( str.charCodeAt(x+1)!=str.charCodeAt(x)+1 ){
    a=String.fromCharCode(str.charCodeAt(x)+1);
    console.log(a)  
    return a;  
  }else {a=undefined;}
}
  console.log(a)
  
}

fearNotLetter("abcdefghjklmno");

It passes the tests, but it can still be better

For example, if the declaration of a would give it the value of undefined you can remove the else statement

1 Like

thanks for the tips!
any help is appreciated.

also, while I’m thinking about these algorithms, I sometimes wonder if I should be using any “intermediate” programming stuff, such as Objects, Functions. I tend to go for the basic solution…

Basic is perfect!

You may want to try to come up with two or three different solutions for these things if you feel like it, to familiarise with different things