简体   繁体   中英

Indexing JavaScript with anonymous function gives an error: "Undefined"

My problem is to retreive 'A', 'B', 'C' or 'D', if the first letter of my passed string s, belongs to {a,e,i,o,u} or {b,c,d,f,g} or {h,j,k,l,m} or other remaining, respectively. I am trying to achieve the same like this:

function getLetter(s){
        let letter = 'ABCD'[findIndex(s[0])];
        return letter;
    }
function findIndex(t){
        var a = Number.parseInt("aeioubcdfghjklmnpqrstvwxyz".indexOf(t)/5);
        //console.log(a);
        if (a<3){
            return a;
        }
        else{
            return 3;
        }
    }

And it worked. But when I tried to reduce the number of lines using an anonymous function, I can't get my function executed. I know I have a silly error, as I new to programming. What is wrong with my code? with something like this:

let getLetter = s =>'ABCD'[function(){
        var a = Number.parseInt("aeioubcdfghjklmnpqrstvwxyz".indexOf(s[0])/5);
        console.log("anonymous function executed");
        if (a<3){
            return a;
        }
        else{
            return 3;
        }
    }]
My output is: undefined

Thanks!

It's because in your original code you are executing findIndex() , but in your second code you are simply using the anonymous function itself as index, you are not executing it.

 let getLetter = s =>'ABCD'[function(){ var a = Number.parseInt("aeioubcdfghjklmnpqrstvwxyz".indexOf(s[0])/5); console.log("anonymous function executed"); if (a<3){ return a; } else{ return 3; } }()] console.log(getLetter("a"));

in your case you only write the function defenition. to run a function you need to add (). so just add () after the defenition.

 let getLetter = s =>'ABCD'[function(){ var a = Number.parseInt("aeioubcdfghjklmnpqrstvwxyz".indexOf(s[0])/5); console.log("anonymous function executed"); if (a<3){ return a; } else{ return 3; } }()]; console.log(getLetter('a'));

In the first example, you called your findIndex method, but in the second one, you just defined the function , without calling it.

You have to solutions to fix it:

  1. to use IIFE ( Immediately Invoked Function Expression ):

where you define and call your method, like what your trying to do here, check the documentation .

 let getLetter = s =>'ABCD'[(function(){ var a = Number.parseInt("aeioubcdfghjklmnpqrstvwxyz".indexOf(s[0])/5); console.log("anonymous function executed"); if (a<3){ return a; } else{ return 3; } })()]; console.log(getLetter('e'))

  1. define findIndex as a seperated function :

Which makes your code clearer.

 let getLetter = s =>'ABCD'[_findIndex(s)]; let _findIndex = letter => { var a = Number.parseInt("aeioubcdfghjklmnpqrstvwxyz".indexOf(letter[0])/5); console.log("anonymous function executed"); if (a<3){ return a; } else{ return 3; } }; console.log(getLetter('e'))

You just pass the reference of function, you didn't execute it. you can add () immediately after the function ends to run it like this:

 let getLetter = s =>'ABCD'[function(){ var a = Number.parseInt("aeioubcdfghjklmnpqrstvwxyz".indexOf(s[0])/5); console.log("anonymous function executed"); if (a<3){ return a; } else{ return 3; } }()] console.log(getLetter('test'));

But I highly recommend not to do that, because it's harder to read, understand, maintain, and develop when time passes. the more important than decrease code lines is it to be human-readable, like Martin Fowler say

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM