Missing Letter - Help needed

Hi,

New to posting so sorry if this is in the wrong place!
I am looking for help, my code so far for the missing letters challenge doesnt work and I cant work out why.
I have it returning the missing letter in a string of letters when it passes a test in a map function but also returning ‘undefined’ when my test for the missing letter isnt passed, I dont know how to stop this?

(I know there are easier ways to do this having looked at the hints but would appreciate understanding why my way doesnt work)

Your code so far


function fearNotLetter(str) {
let fear = {
 'a':1,
 'b':2,
 'c':3,
 'd':4,
 'e':5,
 'f':6,
 'g':7,
 'h':8,
 'i':9,
 'j':10,
 'k':11,
 'l':12,
 'm':13,
 'n':14,
 'o':15,
 'p':16,
 'q':17,
 'r':18,
 's':19,
 't':20,
 'u':21,
 'v':22,
 'w':23,
 'x':24,
 'y':25,
 'z':26}

let str1=str.split("");

return str1.map(function(val,index,array){ 

if((fear[val])!== (fear[array[index+1]]-1) && (str1.length-1>index)) 
{return String.fromCharCode(val.charCodeAt()+1)}
else return;
});


 
}

console.log(fearNotLetter("abce"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Missing letters

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

Hello and welcome to the forum :partying_face:!

I checked Your code and it works almost as expected.

Your main problem is that the code must return a single letter or undefined, whereas now You’re returning an array which may have only null values:

// Your current output for a string 'abce':
[ null, null, null, 'd', null]

// The expected output for the same string:
'd'

// Your output for the string 'abc':
[ null, null, null ]

// The expected output:
undefined

Basically, You need to iterate the resulting map and remove all the null values. If there are only nulls, then return undefined.

Hope it helps (I’m not giving You the answer yet :stuck_out_tongue:) ,

Regards!

1 Like

Thanks that makes a lot of sense!

I see what you mean about returning an array and not a single answer.

I will try again , Thanks :smile:

1 Like