www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] Help with functions 3

  1. #1
    Join Date
    Nov 2013
    Posts
    27

    resolved [RESOLVED] Help with functions 3

    Hey, please can someone help me work out a code for this function.

    Name of function and its parameter
    match(string,pattern)

    Description
    returns true if all the individual LETTERS of pattern appear in string (regardless of order) (case insensitive)

    Examples
    match("abcdef","@C2D!") returns true
    match("abcdef","CAfe") returns true
    match("abcdef","CG") returns false

    Thanks!

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,259
    That's the problem.

    Where is your initial attempt at a solution?
    Will be glad to help you out, but will not do homework for you.

  3. #3
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    225
    Hope this is what you're looking for:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    	<meta charset="UTF-8" />
    	<script>
    		function testLetter(string,pattern)
    		{
    			for (var i = 0; i < pattern.length; i++)
    			{
    				var tmpChar = pattern.charAt(i);
    				if (string.indexOf(tmpChar) == -1) return false;
    			}
    			return true;
    		}
    	</script>
    </head>
    <body>
    </body>
    </html>

  4. #4
    Join Date
    Nov 2013
    Posts
    27
    Quote Originally Posted by tech_soul8 View Post
    Hope this is what you're looking for:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    	<meta charset="UTF-8" />
    	<script>
    		function testLetter(string,pattern)
    		{
    			for (var i = 0; i < pattern.length; i++)
    			{
    				var tmpChar = pattern.charAt(i);
    				if (string.indexOf(tmpChar) == -1) return false;
    			}
    			return true;
    		}
    	</script>
    </head>
    <body>
    </body>
    </html>
    Hey, I tried the code but it didn't work too well. Its meant to show true, true, false but it showed false, false, false.
    I changed some things but it still gives me pretty much the same answer.

    This gives me true, true, true

    Code:
    function match(string,pattern)
    		{
              var
    					index = true,
    					i;
    
    				for (i = 0; i < pattern.length; i++) {
    					if (pattern.toLowerCase().indexOf(string) !== -1) {
    						index = i;
    						break;
    					}
    				}
    				return index;
              
    		}
    
    alert(match("abcdef","@C2D!"));
    alert(match("abcdef","CAfe"));
    alert(match("abcdef","CG"));
    and this gives me false false false

    Code:
    function match(string,pattern)
    		{
    			for (var i = 0; i < pattern.length; i++)
    			{
    				var tmpChar = pattern.charAt(i);
    				if (string.indexOf(tmpChar) == -1) return false;
    			}
    			return true;
    		}
    
    alert(match("abcdef","@C2D!"));
    alert(match("abcdef","CAfe"));
    alert(match("abcdef","CG"));

  5. #5
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    778
    Hi, this is moderately tested, but gives the results they are looking for:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<body>
    
    		<script type="text/javascript">
    
    			function match(string, pattern)
    			{
    				var i, j;
    
    				string = String(string).toLowerCase();
    				for (i = 0, j = String(pattern).toLowerCase().replace(/[^a-z]/g, ''); i < j.length; ++i) {
    					if (string.indexOf(j.charAt(i)) < 0) {
    						break;
    					}
    				}
    				return i >= j.length;
    			}
    
    			alert(match('abcdef', '@C2D!')); // returns true
    			alert(match('abcdef', 'CAfe')); // returns true
    			alert(match('abcdef', 'CG')); // returns false
    
    		</script>
    
    	</body>
    </html>
    JavaScript: Learn | Validate | Compact

  6. #6
    Join Date
    Nov 2013
    Posts
    27
    Its perfect, Thank you!! You're probably going to fight with tech_soul8 later on. lol

  7. #7
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    778
    I'll beg for mercy if need be
    JavaScript: Learn | Validate | Compact

  8. #8
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    225
    I haven't understood it quite well and Championship league football match was about to start... but bionoid solved it nicely.

    Quote Originally Posted by CodeGuru View Post
    Its perfect, Thank you!! You're probably going to fight with tech_soul8 later on. lol
    Now we're even

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles