www.webdeveloper.com
Results 1 to 5 of 5

Thread: Javascript help

  1. #1
    Join Date
    Nov 2013
    Posts
    3

    Javascript help

    What it is supposed to do - Example

    url1(pages,"ALT") returns "www.xyz.ac.uk"
    url1(pages,"xyz") returns ""
    The error - ReferenceError: ALT is not defined

    var pages = [ "|www.lboro.ac.uk|Loughborough University offers degree programmes and world class research.", "!www.xyz.ac.uk!An alternative University" , "%www%Yet another University"];

    alert(url1(pages, ALT));

    function url1(pages,pattern) {
    var siteContent = [];
    for(i=0;i<pages.length;i++) {
    var seperator = pages[i].charAt(0);
    if(pages[i].indexOf(seperator)>0){
    siteContent = pages[i].split(pages[i].indexOf(seperator));
    }
    if( index(siteContent[2],pattern,false)>=0){
    return siteContent[1];
    }else{
    return "";
    }

    }
    }

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,508
    first off, please wrap any code in code tags, it makes life easier.

    second, your code makes no sense, you have the wrong use of a method that is not itself written properly as in its missing the Of.

    So you haven't copied and pasted this, you must have types this.

    What is the real objective of your homework? Troubleshoot / Debug, correct errors to get a working script? Please expand on your very limited explanation because looking at the array, it appears to me that your goal if you are to return something from the function is to get the ALT of the paring and your delimiter is different in each case and your main objective is to find that delimiter in one of those array elements and return a text string and not the URL.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Nov 2013
    Posts
    3
    Quote Originally Posted by \\.\ View Post
    first off, please wrap any code in code tags, it makes life easier.

    second, your code makes no sense, you have the wrong use of a method that is not itself written properly as in its missing the Of.

    So you haven't copied and pasted this, you must have types this.

    What is the real objective of your homework? Troubleshoot / Debug, correct errors to get a working script? Please expand on your very limited explanation because looking at the array, it appears to me that your goal if you are to return something from the function is to get the ALT of the paring and your delimiter is different in each case and your main objective is to find that delimiter in one of those array elements and return a text string and not the URL.
    I have to write a function that does this - 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 ""

  4. #4
    Join Date
    Mar 2009
    Posts
    523
    If I understand you correctly something like this is what you are looking for:

    Code:
    function url1(pages, str){
       var i, len, address, sep, data, arr;
       len = pages.length;
       str = str.toLowerCase();
       for(i = 0; i < len; i++){
          sep = pages[i].substr(0,1);
          arr = pages[i].split(sep);
          address = arr[1];
          data = arr[2].toLowerCase();
          if(data.indexOf(str) != -1){
              return address;
          }
       }
       return "";
    }

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,508
    You have to learn the difference between a string and a variable when passing to a function or using in a script.

    alert(url1(pages, ALT)); Vs alert(url1(pages, "ALT")); which the second one should not return anything if you have no delimiter that is ALT

    Looking at this var pages = [ "|www.lboro.ac.uk|Loughborough University offers degree programmes and world class research.", "!www.xyz.ac.uk!An alternative University" , "%www%Yet another University"]; your delimiters appear to be | ! and %.

    So your objective really is to

    Look at each entry in the array, test for the passed string to see if it exists, that means you grab the first character at position 0 in the string. You could test the first character to see if it matches if you dont want to grab the first character.

    You are if you have a match meant to return the description, not the address.

    Is that correct?
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

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