About return String.fromCharCode();

About return String.fromCharCode();
0.0 0

#1

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!!!


#2

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.


#3

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.


#4

I typically use what makes the code more readable. If a map or reduce will make the code more readable, then I will use those most of the time. It can be argued that basic for and while loops are more efficient than using higher order functions like map and reduce, because you are invoking an a function call during each iteration. However, the higher order functions can make code more readable, because you can focus on what you want a function to do and not get bogged down in how to iterate through an array other values associated with while and for loops. The higher order functions take care of the iteration for you when you have an iteratable object with which to start.