Recursive function to find first alphabetic character

I have Javascript code where I take a large piece of text, separate it at each period, load it into an array, and remove any html tags using replace(). Then, I need to use a recursive function to find the first alphabetic character in the string because I want to put specific formatting on the first alphabetic character. To me, it should be at substring(0,1)…

…the problem is, I came across this issue where the first alphabetic character is usually substring(2,3) but not always. So I wrote the recursive function below. The problem is the character at 0,1,2, and sometimes 3 is not a blank space but it’s not an alphabetic character. If I do eval(), it’s ‘undefined’. But my recursive function is failing when I eval() after the ‘undefined’ characters. I’m missing something but not sure what.

Any ideas how I can test for those first characters that are ‘undefined’? I suppose they are a result of the replace().

function getFirstLetter(text,count){
    // If first time calling the function, count will be null so set to 0
    if(count == null){
        count = 0; 
    }

    // Get the character at the count index
    thisChar = eval(text.charAt(count));

    // Is this character the first alphabetic character? If so, return
    // Here's where the problem occurs...I've noticed that the function
    // will return 'undefined' for the first few non-alphabetic characters
    // but then it stops at the first alphabetic character, no idea why
    if(thisChar != 'undefined' && thisChar != null){
        return count; 
    }else{
        // If not, call the function again for the next index
        getFirstLetter(text,count+1); 
    }
}

Example array text:

Here is a line of text, Here is the next line, And the next line

But if I check each of the first characters, I don’t get an alphabetic character or a blank space. When I do alert(array.charAt(0)) I get an alert with nothing in it. There’s no blank space in the alert.

Answer

What you probably want is a method utilising a regular expression like this:

function getFirstLetter(text){
  return text.search(/[a-zA-Z]/);
}

There are a few problems with your code:

  1. If an argument is missing (e. g. count), it’ll be undefined in the function, not null
  2. Checking for the string 'undefined' instead of the intended special value undefined
  3. Using eval seems kinda hacky and counter-intuitive in this case. Use the short-circuit OR operator || instead; then you only need one comparison — thisChar != null — anyways

Your code improved altogether:

function getFirstLetter(text,count){
    if(count == undefined){
        var count = 0; 
    }
    var thisChar = text.charAt(count)||null;
    if(thisChar != null){
        return count; 
    }
    else{
        getFirstLetter(text,count+1); 
    }
}

What you are left with, unfortunately, is a useless function that will either return a number or abort with an InternalError: too much recursion because either charAt( n ) will have a low enough n to return that character at n always or it will increase the n and always return '' and therefore null.

Therefore, stick with the RegExp solution above.

Leave a Reply

Your email address will not be published. Required fields are marked *