www.webdeveloper.com
Results 1 to 12 of 12

Thread: [RESOLVED] Help with functions 6

  1. #1
    Join Date
    Nov 2013
    Posts
    27

    resolved [RESOLVED] Help with functions 6

    Hey, I was working on this but I'm kinda stuck. Can someone help me out please?

    This is the initial question:

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

    Description
    returns an array of urls from pages for which the corresponding content matches pattern

    I worked out a code but it crashed somewhere

    Code:
    function urlpart(s)
    {
        //function to find seperator 
        var seperator = s[0];
        var seperatorfind = s.lastIndexOf(seperator);
     //   var endofurl = s.charAt(s.length);
        
        var theurl = s.substring(1,seperatorfind);
            
        
        return theurl;
    } 
    
    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 urls(pages,pattern); {
    
    var matchurls= [];
    
    for (var i=0;i< pages.length;i++)
    {
    
    if(match(content(pages[i],pattern)));
    
    matchurls[matchurls.length]= pages[i];
    
    
    return matchurls;  
    }
    else 
     return " " ;
    
    }
    
    alert(urls(pages,"an"));
    alert(urls(pages,"un"));
    I don't think it is correct so I'm trying to change the format and make the code smaller. A little advice will be appreciated!

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    225
    If case doesn't play any role and if I get it right then you should try this code:

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    	<title> Untitled </title>
    	<meta charset="utf-8">
    	<script>
    		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 url(pages,pattern)
    		{
    			var re = /w+[.a-z]+uk/gi, i, urlName = [];
    			
    			for (i = 0; i < pages.length; i++)
    			{
    				if (pages[i].indexOf(pattern) != -1)
    					if (re.test(pages[i]) != false)
    						urlName[urlName.length] = pages[i].match(re);
    			}
    			if (urlName.length)
    				return urlName.toString();
    			else
    				return "No match found!!";
    		}
    		alert(url(pages,"un"));
    		alert(url(pages,"an"));
    	</script>
    </head>
    <body>
    </body>
    </html>
    Wasn't sure if ""www" counts as a link? It shouldn't because it is not a valid link. Also wasn't sure if you are going to "target" pattern when it comes to case because you know what's in those arrays.
    Last edited by tech_soul8; 11-07-2013 at 11:54 AM.

  3. #3
    Join Date
    Nov 2013
    Posts
    27
    Hmm, I'll make a few adjustments to the code. I probably made mistakes somewhere inbetween. Thanks!

  4. #4
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    225
    Code:
    alert(url(pages,"Un")) // returns: www.liv.ac.uk, www.xyz.ac.uk
    alert(url(pages,"An")) // returns: www.xyz.ac.uk
    If any of the pattern is matched on the third element of "pages" array you shouldn't get any value returned because third element doesn't contain any valid link. As I stated above in the previous reply a result you'll get from the first and second element in pages array depends on case.

  5. #5
    Join Date
    Nov 2013
    Posts
    27
    I know but
    Code:
    alert(url(pages,"Un"))
    returns "No match found!!"

  6. #6
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    225
    Did you change anything? Just tested it (Chrome), returns: www.liv.ac.uk, www.xyz.ac.uk.

  7. #7
    Join Date
    Nov 2013
    Posts
    27
    Nope, i didn't change anything. Hmm that's strange. It should be okay though because "un" isn't in the variable "pages"

  8. #8
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    225
    Hm...I just tried some tests and it looks like "test()" method returns some strange results... Sorry for that, now I'll have to post some questions

  9. #9
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    225
    Change above code to this one, should work now though doesn't know why...

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    	<title> Untitled </title>
    	<meta charset="utf-8">
    	<script>
    		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 url(pages,pattern)
    		{
    			var re = /w+[.a-z]+uk/, i, urlName = [];
    			
    			for (i = 0; i < pages.length; i++)
    			{
    				if (pages[i].indexOf(pattern) != -1)
    					if (re.test(pages[i]) != false)
    						urlName[urlName.length] = pages[i].match(re);
    			}
    			if (urlName.length)
    				return urlName.toString();
    			else
    				return "No match found!!";
    		}
    		alert(url(pages,"Un")); // returns: www.liv.ac.uk, www.xyz.ac.uk
    		alert(url(pages,"An")); // returns: www.xyz.ac.uk
    	</script>
    </head>
    <body>
    </body>
    </html>

  10. #10
    Join Date
    Nov 2013
    Posts
    27
    Okay it works now. Thanks again!

  11. #11
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    778
    Sorry I wasn't around last night, I had no power at home.
    My attempt:

    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 urls(pages, pattern)
    			{
    				var
    					i, data,
    					results = [];
    
    				for (i = 0; i < pages.length; ++i) {
    					data = pages[i].split(pages[i].charAt(0));
         				if ((data[2].indexOf(pattern) > -1) && (data[1].split('.').length > 1)) {
    						results.push(data[1]);
    					}
    				}
    				return results.length ? results : 'No match found!!';
    			}
    
    			alert(urls(pages, 'Un')); // returns: www.liv.ac.uk, www.xyz.ac.uk
    			alert(urls(pages, 'An')); // returns: www.xyz.ac.uk
    			alert(urls(pages, 'Ex')); // returns: No match found!!
    
    		</script>
    
    	</body>
    </html>
    Last edited by bionoid; 11-08-2013 at 01:06 AM. Reason: Modified code to include "No Match Found", not sure if it was a requirement??
    JavaScript: Learn | Validate | Compact

  12. #12
    Join Date
    Nov 2013
    Posts
    27
    Well it might come in handy. I think im meant to have 3 values so thats alright. Thanks!

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