Return statement always returning undefined

Tell us what’s happening:
Challenge: Missing letters
The return statement is not working. In the code below, I see that console.log(res) prints desired value to console. But, undefined is returned for every case. I cannot understand what is the problem. Please, tell me the mistake in code.

  **Your code so far**

function fearNotLetter(str) {
str.split('').map((ele, ind, arr)=>{
  if(ind!==0 && str.charCodeAt(ind)!=(str.charCodeAt(ind-1)+1)){
    const res=String.fromCharCode(str.charCodeAt(ind-1)+1);
    console.log(res);  //Prints expected outputs
    return res;              //Always returns undefined :(

console.log(fearNotLetter("abce"));  //Prints undefined
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0.

Challenge: Missing letters

Link to the challenge:

The returned value of each map() iteration is returned to the map() method to construct a new array, not to the outer function, meaning your function isn’t actually returning anything currently.

Also note that map() spits out a new array, which isn’t what the challenge is asking for.


Note that a map should not be used as a general purpose loop. The only reason you should use a map is to make a new array from the elements of an old array.

1 Like

You’re a. not returning anything and b. misusing map (it isn’t an alternative to a loop). It is a function that takes an array + a callback function, producing a new array that’s the same length, filled with the results of running the callback function on each item in the array.

So if you don’t return the result of running map, then nothing is returned. The return in your code is for the callback function – literally a different function – but the function fearNotLetter doesn’t return anything.

  • The function that you give to map goes through each element in turn.
  • If the char code of the current element doesn’t match the previous one + 1, it returns the previous one + 1 as a character. Otherwise it returns undefined.
  • split("").map("abce") runs, but doesn’t do anything meaningful, because you aren’t returning anything.
  • So if you add a return: ie return split("").map("abce"), then the function will return [undefined, undefined, undefined, "d"].
  • That callback function runs. “a” is at position 0, return undefined so map puts undefined at position 0. “b” is at position 1, but “a” + 1 is “b”, return undefined so map puts undefined at position 1. “c” is at position 2, but “b” + 1 is “c”, return undefined so map puts undefined at position 2. “e” is at position 3, “c” + 1 is “d”, so return “d” so map puts "d" at position 3.
  • Then control is returned to the main fearNotLetter function, which returns the result: [undefined, undefined, undefined, "d"].
  • There are array functions that are designed for iterating over an array and searching for a specific value (find would work), and there is an array function designed for side effects (forEach – you could set up a variable, run forEach and assign the missing letter to the variable when you get to it), or you can just loop over the string.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.