...

View Full Version : Using a function to create a "Case IN-SENSITIVE" search



georgesofroniou
11-09-2010, 05:47 PM
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. :confused:
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

Philip M
11-09-2010, 06:14 PM
You seem to be making a meal of it.


<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:-


<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.

Logic Ali
11-09-2010, 07:07 PM
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.


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) );

georgesofroniou
11-09-2010, 07:48 PM
thanks for the replies :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum