About return String.fromCharCode();

Hello everyone, I’m sure this is a stupid question but when I was working on Missing Letters Challenge and try to return String.fromCharCode(value) doesn’t work but if I storage the return from String.fromCharCode(value) in a variable and then return the variable, work. My question is, Can somebody explain to me why??? Thanks!!!

This code work:

function fearNotLetter(str) {
  var missingLetter;
  var inputLetterRange = str.split('');
  var asciiInputLetterRange = inputLetterRange.map(function(letter){
    return letter.charCodeAt();
  });
 
  
  
  asciiInputLetterRange.forEach(function(letterValue, index, asciiInputLetterRange){
  if (index < asciiInputLetterRange.length - 1){
    if (letterValue != asciiInputLetterRange[index + 1] - 1){
      missingLetter = String.fromCharCode(letterValue + 1);
    }
  }
  });
  
  return missingLetter;
}

fearNotLetter("abce");

return “d”;

This doesn’t pass the test and return is empty in the codemirror.

function fearNotLetter(str) {
  var inputLetterRange = str.split('');
  var asciiInputLetterRange = inputLetterRange.map(function(letter){
    return letter.charCodeAt();
  });
 
  
  
  asciiInputLetterRange.forEach(function(letterValue, index, asciiInputLetterRange){
  if (index < asciiInputLetterRange.length - 1){
    if (letterValue != asciiInputLetterRange[index + 1] - 1){
      return String.fromCharCode(letterValue + 1);
    }
  }
  });
  
  return undefined;
}

fearNotLetter("abce");

Thanks!!!

Just looking at the above section of code, let’s walk through each iteration of the forEach.

1st iteration - index = 0, so first if statement evaluates to true. Second if statement evaluates to false, because letterValue = 97 and asciiInputLetterRange[0 + 1] - 1 = 97.

2nd iteration - index = 1, so first if statement evaluates to true. Second if statement evaluates to false, because letterValue = 98 and asciiInputLetterRange[1 + 1] - 1 = 98.

3rd iteration - index = 2, so first if statement evaluates to true. Second if statement evaluates to true, because letterValue = 99 and asciiInputLetterRange[2 + 1] - 1 = 100. Since the second if statement evaluates to true, your code attempts to execute the following:

return String.fromCharCode(letterValue + 1);

Unfortunately, a return statement inside a forEach loop just breaks out of the forEach loop and not the actual function. As soon as the forEach is exited, the return undefined; does execute and return the value undefined to the calling function.

It would be better to use a plain for loop instead of forEach if you want to return a value to the calling function.

1 Like

Thaks a lot!!! Can’t be more clear, a return statement inside a forEach loop just breaks out of the forEach loop and not the actual function.

I like to use for loops because allow me to have more control but I usually read people recommending use built-in function like .forEach(), .map() , .reduce() , etc.

Is anything wrong to use for loops in production???

I know, this can go deep, XD

Thanks a lot and sorry to bother you.