Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts

    check if string starts with more than 2 of a letter

    Hi

    How do you check if a string starts with more than 2 in a row of "a"



    "ab" //this is fine
    "aab" // this bad
    "aaaaaaaaaaaaaaaaaaaaaab" //this is bad


    thanks for the help

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    if (str.charAt(0) == "a" && str.charAt(1) == "a")alert("too many a's")

  • Users who have thanked DaveyErwin for this post:

    code-in-time (05-31-2012)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by DaveyErwin View Post
    if (str.charAt(0) == "a" && str.charAt(1) == "a")alert("too many a's")
    I image that he wants to detect two consecutive characters the same (ignoring case), not just two a's. But some words do start with letters the same, oolite, ooze, Llandudno, eel, eery, llama, aardvark and many others.

    Code:
    <script type = "text/javascript">
    
    function twoConsecutiveChars(str) {
    for (var i = 0; i < 1; i++)	{
    if (str.charAt(i).toLowerCase() == str.charAt(i+1).toLowerCase()) {
    alert ("String starts with two " + str.charAt(i).toLowerCase() + " in a row");
    }
    }
    }
    
    twoConsecutiveChars("Bbbcccdeeeffghjj");
    
    </script>
    "Utility is when you have one telephone, luxury is when you have two, opulance is when you have three, and paradise when you have none." - Doug Larson
    Last edited by Philip M; 05-31-2012 at 03:39 PM. Reason: Spelling

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    code-in-time (05-31-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •