www.webdeveloper.com
Results 1 to 2 of 2

Thread: Help with functions 5

  1. #1
    Join Date
    Nov 2013
    Posts
    27

    Help with functions 5

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

    Name of function and its parameter
    url1(pages,pattern)

    Description
    returns the url part of the first page in pages for which the content part matches pattern (in the style of function 1, case insensitive)
    returns an empty string if no page is found

    Example
    url1(pages,"ALT") returns "www.xyz.ac.uk"
    url1(pages,"xyz") returns ""

    Thanks!

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Code:
    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<body>
    
    		<script type="text/javascript">
    
    			var pages = ['|www.liv.ac.uk|Liverpool University offers degree programmes and world class research.', '!www.xyz.ac.uk!An alternative University' , '%www%Yet another University'];
    
    			function url1(pages, pattern)
    			{
    				var
    					i, data;
    
    				pattern = pattern.toLowerCase();
    
    				for (i = 0; i < pages.length; ++i) {
    					data = pages[i].split(pages[i].charAt(0));
    					if (data[2].toLowerCase().indexOf(pattern) > -1) {
    						return data[1];
    					}
    				}
    				return '';
    			}
    
    			alert(url1(pages, 'ALT')); // returns 'www.xyz.ac.uk'
    			alert(url1(pages, 'xyz')); // returns ''
    
    		</script>
    
    	</body>
    </html>
    JavaScript: Learn | Validate | Compact

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