www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Help with functions 4

  1. #1
    Join Date
    Nov 2013
    Posts
    27

    resolved [RESOLVED] Help with functions 4

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

    Name of function and its parameter
    matchContents(contents,pattern)

    Description
    returns an array of indexes of each page in contents that match pattern (match in the style of function 3)

    Examples
    matchContents(contents,"LU") returns [0,1]
    matchContents(contents,"other") returns [0,2]

    Thanks!

  2. #2
    Join Date
    Nov 2013
    Posts
    27
    Quote Originally Posted by CodeGuru View Post
    Hey, Please can someone help me work out a code for this function

    Name of function and its parameter
    matchContents(contents,pattern)

    Description
    returns an array of indexes of each page in contents that match pattern (match in the style of function 3)

    Examples
    matchContents(contents,"LU") returns [0,1]
    matchContents(contents,"other") returns [0,2]

    Thanks!
    A bit silly to put this with no information of what I've done so far but here it is and I think it works well.

    Code:
    var contents = [ "Lancaster University offers degree programmes and world class research.", "An alternative University", "Yet another University"];
    
    function match(string, pattern){
        var string= string.toUpperCase();
        var pattern= pattern.toUpperCase();
            for(var k= 0; k< pattern.length; k++){
        if(string.indexOf(pattern[k])<0)
        return false;
            }
        return true;  }
    
    function matchContents(contents,pattern)
    { var index=[];
      for(var i=0;i<contents.length;i++)
      {
        if(match(contents[i],pattern))
          index[index.length]=i;
          }
        return index;
    }
    
    alert("["+ matchContents(contents,"LU")+"]"); //returns [0,1]
    alert("["+ matchContents(contents,"other")+"]"); //returns [0,2]
    If there are any adjustments I can make please let me know!

  3. #3
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    776
    Congrats if you got it to work without help.
    Here is the single function version:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<body>
    
    		<script type="text/javascript">
    
    			var contents = ['Lancaster University offers degree programmes and world class research.', 'An alternative University', 'Yet another University'];
    
    			function matchContents(contents, pattern)
    			{
    				var
    					results = [], string,
    					i, j;
    
    				pattern = pattern.toLowerCase();
    				for (i = 0; i < contents.length; ++i) {
    					string = contents[i].toLowerCase();
    					for (j = 0; j < pattern.length; ++j) {
    						if (string.indexOf(pattern.charAt(j)) < 0) {
    							break;
    						}
    					}
    					if (j >= pattern.length) {
    						results.push(i);
    					}
    				}
    				return results;
    			}
    
    			alert(matchContents(contents, 'LU')); //returns [0,1]
    			alert(matchContents(contents, 'other')); //returns [0,2]
    
    		</script>
    
    	</body>
    </html>
    JavaScript: Learn | Validate | Compact

  4. #4
    Join Date
    Nov 2013
    Posts
    27
    I did ^_^ I'm learning well! Thanks again

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