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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Need help explaining what the following function is doing

    I understand the concept of the function which is to return true if the pattern appears in the string.
    How do i make this code case insensitive and can someone explain to me what the steps are for the function Thanks

    Code:
    function match(string,pattern)
    {for(var i = 0; i<pattern.length; ++i)
    var a = pattern.charAt(i);
    if(string.indexOf(a) == -1)
    return false;
    else return true;
    }
    I dont understand how it is functioning and how i can make it case sensitive using the .TolowerCase string object

    Thanks
    Last edited by nervousprogramm; 11-05-2012 at 10:30 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    the concept of the function which is to return true if the pattern appears in the string
    Nonsense. It does nothing of the kind!

    In point of fact, it's a fairly useless little function.

    If you call it with (for example) match( "zamboni", "imabozn") it will return true because it finds every letter of "imabozn" *SOMEWHERE* in "zamboni".

    Whoopee.

    And it already *IS* case sensitive.

    Calling with match( "zamboni", "imBozn" ) will return false because "B" won't be found.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Nonsense. It does nothing of the kind!

    In point of fact, it's a fairly useless little function.

    If you call it with (for example) match( "zamboni", "imabozn") it will return true because it finds every letter of "imabozn" *SOMEWHERE* in "zamboni".

    Whoopee.

    And it already *IS* case sensitive.

    Calling with match( "zamboni", "imBozn" ) will return false because "B" won't be found.
    yes thats correct but how do i go about making this case insensitive*

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    NO NO NO!

    I am wrong! The function isn't even as useful as I described it!

    In point of fact, all it will *REALLY* do is find out if the LAST CHARACTER of the pattern is ANYWHERE in the string.

    So match( "zamboni", "abcdefghi" ) will return true while match( "zamboni", "zambonizambonizambonix" ) will return false.

    Hard to imagine a more useless function.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    In short it does not work - it finds the match between the final character in the pattern (only) anywhere in the string. Useless!.
    There is no need for a loop. This is what you want:_


    Code:
    <script type = "text/javascript">
    
    function match(string,pattern) {
    string = string.toLowerCase();  // make case insensitive
    pattern = pattern.toLowerCase();
    if (string.indexOf(pattern) == -1) {   // pattern not found
    return false;
    }
    else {return true};
    }
    
    alert (match("HELLO", "hell"));
    alert (match("HELLO", "hll"));
    
    </script>

    Do not omit the braces around if and else and for statements. That will lead you into trouble one day - rather soon.


    The fool who knows his foolishness, is wise at least so far. But a fool who thinks himself wise, he is called a fool indeed.
    Last edited by Philip M; 11-05-2012 at 10:08 PM. Reason: Typo in code

    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:

    nervousprogramm (11-05-2012)

  • #6
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    NO NO NO!

    I am wrong! The function isn't even as useful as I described it!

    In point of fact, all it will *REALLY* do is find out if the LAST CHARACTER of the pattern is ANYWHERE in the string.

    So match( "zamboni", "abcdefghi" ) will return true while match( "zamboni", "zambonizambonizambonix" ) will return false.

    Hard to imagine a more useless function.
    well the goal i am trying to achieve is to create a function which will return true if the pattern exist in the string regardless of the order and it needs to be case insensitive any tips on how i should tackle this?

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    If you REALLY wanted to do WHAT YOU DESCRIBED:
    the concept of the function which is to return true if the pattern appears in the string
    then that function as written is a braindead as they come.

    MUCH easier to do WHAT YOU DESCRIBE than what that silly function is doing:
    Code:
    function match(string,pattern) 
    {
        return string.indexOf(pattern) >= 0;
    }
    If you want to make it case insensitive:
    Code:
    function match(string,pattern) 
    {
        return string.toLowerCase().indexOf(pattern.toLowerCase()) >= 0;
    }
    You seem to have somehow come under the impression that indexOf() only allows a one-character string as the argument. Not at all true! indexOf( ) is ALREADY the match function you were trying to create!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    nervousprogramm (11-05-2012)

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    create a function which will return true if the pattern exist in the string regardless of the order
    You mean you *WANT* to return true when you call match("zamboni","bam")???? Or match("ZamBoni","zoozoozoozoo")????
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    You mean you *WANT* to return true when you call match("zamboni","bam")???? Or match("ZamBoni","zoozoozoozoo")????
    yes match("zamboni","bam")

    I now realise the first code was absolutely nonsense
    thanks for pointing it out but how would i go about this

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    In that case:
    Code:
    function funkyMatchLetters( string, letters )
    {
        string = string.toLowerCase();
        letters = letters.toLowerCase();
        for ( var i = 0; i < letters.length; ++i )
        {
            if ( string.indexOf( letters.charAt(i) ) < 0 ) { return false; }
        }
        return true;
    }
    Why in the world would you need such a weird function?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    nervousprogramm (11-05-2012)

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    You said yes to match("ZamBoni","bam") but you didn't say one way or the other to match("ZamBoni","zoozoozoozoo")

    I'm not sure how you would easily get true for the first and get false for the second. I see a way, but it's not easy.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by nervousprogramm View Post
    well the goal i am trying to achieve is to create a function which will return true if the pattern exist in the string regardless of the order and it needs to be case insensitive any tips on how i should tackle this?
    Perhaps he means "regardless of where in the string the pattern is found".
    At the start, embedded in the middle, at the end.

    I don't see how you can match "zamboni" and "bam" forwards, backwards or any other way.
    Last edited by Philip M; 11-05-2012 at 10:13 PM.

    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.

  • #13
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Perhaps he means "regardless of where in the string the pattern is found".
    At the start, embedded in the middle, at the end.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    In that case, Philip's answer in post #5 or miy second answer in post #7 are correct.

    "order" and "location" are not AT ALL the same thing, Nervous.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    nervousprogramm (11-05-2012)

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by nervousprogramm View Post
    In that case both Old Pedant and I have given you suitable code.

    Shall we bring this thread to a close now?

    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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