Missing letters - object key as return output

Tell us what’s happening:
The below code seems to pass the challenge as it returns the exact alphabet the challenge needs but it doesn’t pass. I suspect it is because I return the key of the object which might not count as a string. However when I check the output in the console, my result is not an array or object key, it is shown as a string. Is this a fair answer to the challenge?

Your code so far

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


  for (let i = 0; i < str.length ;i++){
    console.log((alpha[str[i+1]]) + "-" + (alpha[str[i]]) + " deduction is " + (alpha[str[i+1]] - alpha[str[i]]));

    if (alpha[str[i+1]] - alpha[str[i]] == 1 ){
      console.log("we are at " + Object.keys(alpha).filter(function (key){ return alpha[key] === alpha[str[i]]}));
    else if (alpha[str[i+1]] - alpha[str[i]] == 2 ) {
      return Object.keys(alpha).filter(function (key){ return alpha[key] === (alpha[str[i]] + 1)});
    else {
      return undefined;
    console.log("end round" + i);



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:

Huh. I was actually going to come up with all sorts of ways that this was wrong, and this was wrong, and this was WRONG. But oddly, it’s not. You’re REALLY close.

Try logging what you actually return from the fearNotLetter(). You’ll see that you’re returning a single-element array. Can you figure how you might return just the value of that single element? (hint: what is its index within the array…?)

Thanks! snowmonkey! I literally found the solution10 seconds before you reply! It was actually returning an array not a string, so I simply added a [0] after return Object.keys(alpha).filter(function (key){ return alpha[key] === (alpha[str[i]] + 1)}) to let it return the first item in the array. and it worked! Thanks!

Now, one more thing to note: While you are not WRONG, you did re-invent the wheel. String values also have what’s known as ASCII values, or numbers, that can be used to represent them. For example, the ASCII value of a is 97, b is 98, and z is 122.

These can all be found from a character like so:

let myString = "abcdefg";
for (let index = 0; index < myString.length; index++){
  let letter = myString[index],
       code = myString.charCodeAt(index);
  console.log(letter+" has the ascii value "+code+".");

Using charCodeAt(), you already have access to an index number for alphanumerics.

To see this in action, https://jsfiddle.net/snowMonkey/8k5vt97f/6/

I see. I knew there would be a much quicker way than opening an object and manually matching the alphabets into numbers, but I just didn’t know how to! This happens very often to me when I write a very long code to complete a simple challenge not knowing that things can be done with just a pure function like map() or reduce(). But I guess i’ll learn and remember more necessarily functions as I go along the learning path, just like this charCodeAt() that you just introduced to me.

Thank you so much snowmonkey! =]

It’s a process, @mk317 – I often revisit past challenges as my skills evolve, to see if I could do them differently, or more efficiently, or with a more elegant turn of code. We are evolving our brains through learning, and so long as you keep asking, your brain keeps growing!

Best of luck, and glad to help.

1 Like