Missing letters - Return Undefined

Tell us what’s happening:
I can’t seem to able to return undefined when there is no missing letter.

I created an array from the string so that it’s values can act like a counter using forEach.
I then looped through checking the unicode for each letter and subtracting it from the next one.

If the difference between the two is more than 1, I add a single digit to the unicode of the previous character so that I can then convert it back to a letter.

I then set str to the letter after conversion and returned it.

Problem is I can’t figure out the case for returning undefined when no letters are missing.

Your code so far


function fearNotLetter(str) {
  let strA = str.split('');
  strA.forEach(x => {
    if(str.charCodeAt(str.indexOf(x)+1) - str.charCodeAt(str.indexOf(x)) > 1){
     str = String.fromCharCode(str.charCodeAt(str.indexOf(x))+1);
    }

    else{
       str = undefined;
    }
  });
console.log(str);
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/70.0.3538.110 Safari/537.36.

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

  1. Make a new variable for the assignment inside the if statement (instead of str), and return that variable.
  2. In the else just return undefined.
1 Like

Hi @lasjorg. Tried it but didn’t work for me. This is the new edit

function fearNotLetter(str) {
let strA = str.split(’’);
strA.forEach(x => {
if(str.charCodeAt(str.indexOf(x)+1) - str.charCodeAt(str.indexOf(x)) > 1){
var newString = String.fromCharCode(str.charCodeAt(str.indexOf(x))+1);
return newString;
}

else{
   return undefined;
}

});
console.log(newString);
}

fearNotLetter(“abce”);

  1. Move the variable declaration out of the if (declare it below strA), and only do the assignment inside the if.
  2. Return the variable as you did it before (not inside the if).
1 Like

Thanks alot @lasjorg. It worked. I might have to go read more on scope.:slightly_smiling_face: