www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Help with functions 7

  1. #1
    Join Date
    Nov 2013
    Posts
    27

    resolved [RESOLVED] Help with functions 7

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

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

    Description
    returns the number of times that pattern occurs in string (overlapping matches count) (case insensitive)

    Examples
    score("THAT hat!","hAt") returns 2
    score("Coooool!","OO") returns 4

    Thanks!

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Only did this one prior to an attempt because it's getting late, and seemed the more entertaining of the others:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<body>
    
    		<script type="text/javascript">
    
    			function score(string, pattern)
    			{
    				var
    					count = 0,
    					index = -1;
    
    				string  = string.toLowerCase();
    				pattern = pattern.toLowerCase();
    
    				do {
    					index = string.indexOf(pattern, index + 1);
    					if (index > -1) {
    						++count;
    					}
    				}
    				while(index > -1);
    
    				return count;
    			}
    
    			alert(score('THAT hat!', 'hAt')); // returns 2
    			alert(score('Coooool!',   'OO')); // returns 4
    
    		</script>
    
    	</body>
    </html>
    JavaScript: Learn | Validate | Compact | bionoid

  3. #3
    Join Date
    Nov 2013
    Posts
    27
    Thank you soo much. You've been really helpful!

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