Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2010
    Location
    Home: Essex, Current Location: Loughborough
    Posts
    21
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Exclamation Using a function to create a "Case IN-SENSITIVE" search

    Hi, I am a beginner in programming however I am close to completing this task for my uni course and I know what method is needed "toLowerCase()" yet I am not sure on where it needs to be placed.
    If i paste in the question and then the coding I have done so far any helped would be greatly appreciated.

    Thanks in advance to everyone who replies


    QUESTION:
    function findAnyU(s) - to discover if string s appears in any of the URLs in the array pages. Gives an alert of s together with 'found' or 'not found' as for find1C. [Note that the search should be case insensitive so that 'lboro' and 'Lboro' and 'LBORO' would all be found in 'www.lboro.ac.uk' - all remaining functions should also be case insensitive in this way]

    Example call: findAnyU("Lboro")
    Example result: "Lboro" found

    MY CODING...

    var pages = [
    "[www.lboro.ac.uk] Loughborough University offers degree programmes and world class research.",
    "[www.XXXX.ac.uk] Loughborough University offers degree programmes and world class research. "
    ];

    function findAnyU (s){

    for(i=1;i<pages.length;i++)
    {
    var a = '"'+s+'"';
    var b = pages[i].indexOf('[');
    var l = pages[i].indexOf(']');
    var t = pages[i].substr(b+1, l);
    var c = t.indexOf(s);
    }
    if (c>=0)
    a+= ' found'
    else
    a+= ' not found'

    return (a)
    }

    alert(findAnyU("Lboro"));



    ***********************************
    Thanks Again Everyone X

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    You seem to be making a meal of it.

    Code:
    <script type = "text/javascript">
    
    var pages = [];
    
    pages[0]= "www.lboro.ac.uk" 
    pages[1] = "www.XXXX.ac.uk" 
    
    function findAnyU(str) {
    for(i=0;i<pages.length;i++){
    var reg1 = new RegExp(str,"gi"); // global search ignore case so will match LboRO etc.
    if (reg1.test(pages[i])) {
    var a = str + " Found " + " in Page " + i;
    }
    
    }
    return (a);
    }
    
    alert(findAnyU("Lboro"));
    </script>
    If regular expressions are too advanced for you, then:-

    Code:
    <script type = "text/javascript">
    
    var pages = [];
    
    pages[0]= "www.lboro.ac.uk" 
    pages[1] = "www.XXXX.ac.uk" 
    
    function findAnyU(str) {
    str = str.toLowerCase();   // note that the string to be found must match exactly in lower case
    for (var i =0; i<pages.length; i++) {
    var c = pages[i].indexOf(str);
    if (c!=-1) {var p = i};
    }
    var a = str + " Found " + " in Page " + p;
    return (a);
    }
    
    alert(findAnyU("Lboro"));
    
    </script>
    BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance and expertise for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.


    Quizmaster: What is 80 percent of 200?
    Contestant: Four.
    Last edited by Philip M; 11-09-2010 at 05:31 PM.

  • Users who have thanked Philip M for this post:

    georgesofroniou (11-09-2010)

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    We know there are better ways to do this, but in your code the errors were:
    You should initialise the loop counter to 0 not 1.
    You need a means of ending the loop if the string is found before the last element.
    To get a sub-string using offsets call substring not substr.

    Code:
    var pages = [
    "[www.lboro.ac.uk] Loughborough University offers degree programmes and world class research.",
    "[www.XXXX.ac.uk] Loughborough University offers degree programmes and world class research. "
    ];
    
    function findAnyU(s, arr)
    {
       var a = s, b, l, t, c = -1;
      
       for( var i = 0; i < arr.length && c == -1 ; i++ )
       {   
        b = arr[i].indexOf('[');
        l = arr[i].indexOf(']');
        t = arr[i].substring(b+1, l).toLowerCase();
        c = t.indexOf( s.toLowerCase() );
       }
       if( c > -1 )
        a+= ' found'
       else
        a+= ' not found'
    
       return a;
    }
    
    alert( findAnyU("Lboro", pages) );

  • Users who have thanked Logic Ali for this post:

    georgesofroniou (11-09-2010)

  • #4
    New Coder
    Join Date
    Nov 2010
    Location
    Home: Essex, Current Location: Loughborough
    Posts
    21
    Thanks
    11
    Thanked 0 Times in 0 Posts
    thanks for the replies


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •