I have created this function where it detects the first letter of the string provided. I was hoping if you guys can take a look, give your opinion about it and help me to improve it.
//This function takes a string and outputs
//its first letter
function letterDetector(str){
//Checking if string is an empty string
if(str === "" || str === undefined)
{return ("Please input an argument");}
var alphabetsArr = "abcdefghijklmnopqrstuvwxyz".split("");
//making user input to lower case
var string = str.toLowerCase().replace(/[^0-9a-z]\d+/gi, '');
var result = alphabetsArr.map(letter => {
if( string.charAt(0) === letter ){
//logging out the letter as upper case
console.log("Your first name letter is" + " " + letter.toUpperCase());
}
});
}
letterDetector("Adam"); //output A
letterDetector("£1%Adam"); //output A
letterDetector(""); or letterDetector(); //output Please input an argument
string.charAt(0).toUpperCase()
after stripping nonalpha characters (w/ special case if string ends up empty)? Why go through an unnecessary loop? \$\endgroup\$ – user1149 Dec 31 '17 at 1:06