...

View Full Version : Need help explaining what the following function is doing



nervousprogramm
11-05-2012, 10:24 PM
I understand the concept of the function which is to return true if the pattern appears in the string.
How do i make this code case insensitive and can someone explain to me what the steps are for the function Thanks


function match(string,pattern)
{for(var i = 0; i<pattern.length; ++i)
var a = pattern.charAt(i);
if(string.indexOf(a) == -1)
return false;
else return true;
}

I dont understand how it is functioning and how i can make it case sensitive using the .TolowerCase string object

Thanks

Old Pedant
11-05-2012, 10:54 PM
the concept of the function which is to return true if the pattern appears in the string

Nonsense. It does nothing of the kind!

In point of fact, it's a fairly useless little function.

If you call it with (for example) match( "zamboni", "imabozn") it will return true because it finds every letter of "imabozn" *SOMEWHERE* in "zamboni".

Whoopee.

And it already *IS* case sensitive.

Calling with match( "zamboni", "imBozn" ) will return false because "B" won't be found.

nervousprogramm
11-05-2012, 10:55 PM
Nonsense. It does nothing of the kind!

In point of fact, it's a fairly useless little function.

If you call it with (for example) match( "zamboni", "imabozn") it will return true because it finds every letter of "imabozn" *SOMEWHERE* in "zamboni".

Whoopee.

And it already *IS* case sensitive.

Calling with match( "zamboni", "imBozn" ) will return false because "B" won't be found.
yes thats correct but how do i go about making this case insensitive*

Old Pedant
11-05-2012, 10:57 PM
NO NO NO!

I am wrong! The function isn't even as useful as I described it!

In point of fact, all it will *REALLY* do is find out if the LAST CHARACTER of the pattern is ANYWHERE in the string.

So match( "zamboni", "abcdefghi" ) will return true while match( "zamboni", "zambonizambonizambonix" ) will return false.

Hard to imagine a more useless function.

Philip M
11-05-2012, 10:59 PM
In short it does not work - it finds the match between the final character in the pattern (only) anywhere in the string. Useless!.
There is no need for a loop. This is what you want:_



<script type = "text/javascript">

function match(string,pattern) {
string = string.toLowerCase(); // make case insensitive
pattern = pattern.toLowerCase();
if (string.indexOf(pattern) == -1) { // pattern not found
return false;
}
else {return true};
}

alert (match("HELLO", "hell"));
alert (match("HELLO", "hll"));

</script>


Do not omit the braces around if and else and for statements. That will lead you into trouble one day - rather soon.


The fool who knows his foolishness, is wise at least so far. But a fool who thinks himself wise, he is called a fool indeed.

nervousprogramm
11-05-2012, 10:59 PM
NO NO NO!

I am wrong! The function isn't even as useful as I described it!

In point of fact, all it will *REALLY* do is find out if the LAST CHARACTER of the pattern is ANYWHERE in the string.

So match( "zamboni", "abcdefghi" ) will return true while match( "zamboni", "zambonizambonizambonix" ) will return false.

Hard to imagine a more useless function.

well the goal i am trying to achieve is to create a function which will return true if the pattern exist in the string regardless of the order and it needs to be case insensitive any tips on how i should tackle this?

Old Pedant
11-05-2012, 11:01 PM
If you REALLY wanted to do WHAT YOU DESCRIBED:


the concept of the function which is to return true if the pattern appears in the string

then that function as written is a braindead as they come.

MUCH easier to do WHAT YOU DESCRIBE than what that silly function is doing:


function match(string,pattern)
{
return string.indexOf(pattern) >= 0;
}

If you want to make it case insensitive:


function match(string,pattern)
{
return string.toLowerCase().indexOf(pattern.toLowerCase()) >= 0;
}

You seem to have somehow come under the impression that indexOf() only allows a one-character string as the argument. Not at all true! indexOf( ) is ALREADY the match function you were trying to create!

Old Pedant
11-05-2012, 11:04 PM
create a function which will return true if the pattern exist in the string regardless of the order


You mean you *WANT* to return true when you call match("zamboni","bam")???? Or match("ZamBoni","zoozoozoozoo")????

nervousprogramm
11-05-2012, 11:07 PM
You mean you *WANT* to return true when you call match("zamboni","bam")???? Or match("ZamBoni","zoozoozoozoo")????

yes match("zamboni","bam")

I now realise the first code was absolutely nonsense
thanks for pointing it out but how would i go about this

Old Pedant
11-05-2012, 11:07 PM
In that case:


function funkyMatchLetters( string, letters )
{
string = string.toLowerCase();
letters = letters.toLowerCase();
for ( var i = 0; i < letters.length; ++i )
{
if ( string.indexOf( letters.charAt(i) ) < 0 ) { return false; }
}
return true;
}

Why in the world would you need such a weird function?

Old Pedant
11-05-2012, 11:09 PM
You said yes to match("ZamBoni","bam") but you didn't say one way or the other to match("ZamBoni","zoozoozoozoo")

I'm not sure how you would easily get true for the first and get false for the second. I see a way, but it's not easy.

Philip M
11-05-2012, 11:10 PM
well the goal i am trying to achieve is to create a function which will return true if the pattern exist in the string regardless of the order and it needs to be case insensitive any tips on how i should tackle this?

Perhaps he means "regardless of where in the string the pattern is found".
At the start, embedded in the middle, at the end.

I don't see how you can match "zamboni" and "bam" forwards, backwards or any other way.

nervousprogramm
11-05-2012, 11:13 PM
Perhaps he means "regardless of where in the string the pattern is found".
At the start, embedded in the middle, at the end.

:thumbsup:

Old Pedant
11-05-2012, 11:17 PM
In that case, Philip's answer in post #5 or miy second answer in post #7 are correct.

"order" and "location" are not AT ALL the same thing, Nervous.

Philip M
11-05-2012, 11:18 PM
:thumbsup:

In that case both Old Pedant and I have given you suitable code. :)

Shall we bring this thread to a close now?

nervousprogramm
11-05-2012, 11:20 PM
thanks for the help you Pros much appreciated

Old Pedant
11-05-2012, 11:21 PM
yes match("zamboni","bam")


But then THIS post of yours, Nervous, does NOT match Philip's suggested criteria in his post #12.

All the letters in "bam" appear in "zamboni", but not in order. Not even in backwards order.

So if you really DO want a match("zamboni","bam") to be true, then we aren't done, at all.

nervousprogramm
11-05-2012, 11:23 PM
regardless of order the individual characters of pattern need to appear in string
e.g "abcdef","CAFE" returns true

nervousprogramm
11-05-2012, 11:31 PM
ok yes it doesnt match the criteria

Philip M
11-05-2012, 11:42 PM
regardless of order the individual characters of pattern need to appear in string
e.g "abcdef","CAFE" returns true

Seems weird, but ...


<script type = "text/javascript">

function match(string,pattern) {
string = string.toLowerCase();
pattern = pattern.toLowerCase();
var s = string.split("");
var p = pattern.split("");
var len = p.length;
var count = 0;
for (var i = 0; i <len; i++) {
for (var j = 0; j < string.length; j++) {
if (p[i] == s[j]) {
count ++;
}
}
}

if (count == len) {
alert ("The match was found");
}

else {
alert ("The match was not found")
}

}


match("abcdefg", "CAFE");
match("abcdefg", "CAFFE");
match("abcdefg", "CAFM");
match ("zamboni", "BAM");


</script>

Would you please explain why you want to do this in any case - what is the actual application?

nervousprogramm
11-05-2012, 11:52 PM
Seems weird, but ...


<script type = "text/javascript">

function match(string,pattern) {
string = string.toLowerCase();
pattern = pattern.toLowerCase();
var s = string.split("");
var p = pattern.split("");
var len = p.length;
var count = 0;
for (var i = 0; i <len; i++) {
for (var j = 0; j < string.length; j++) {
if (p[i] == s[j]) {
count ++;
}
}
}

if (count == len) {
alert ("The match was found");
}

else {
alert ("The match was not found")
}

}


match("abcdefg", "CAFE");
match("abcdefg", "CAFFE");
match("abcdefg", "CAFM");
match ("zamboni", "BAM");


</script>

Would you please explain why you want to do this in any case - what is the actual application?

a search engine project

Philip M
11-05-2012, 11:54 PM
a search engine project

If you say so. I still do not really grasp it. :)

Old Pedant
11-06-2012, 12:06 AM
No, Philip, I don't think that's adequate.

Example: match("abcdefg", "aaa" )

Your code will return true because each of the letters in the pattern is found, so the match count (your count variable) will indeed match your string length (the len variable).

*********

I sure can't imagine what the use a a search engine is that manages to find a match on "bin" in "zamboni". Seems utterly and completely weird to me.

Philip M
11-06-2012, 12:19 AM
A correction is needed but not that!

CORRECTION:-

if (p[i] == s[j]) {
p[i] = ""; // count only once when letter match found
count ++;


match ("My Old Man is a dustman", "dust");




match("abcdefg", "aaa" )

It is unclear to me whether that is considered to be a match or not. My understanding is that regardless of order the individual characters of the pattern need to appear in the string. Which surely they do! It is the equivalent of "do the letters a and a and a occur in abcdef". Why do you think that does not comply with the logic of this strange (weird!) requirement?

He does not want to match the string bin in zamboni. He wants to ascertain if all the letters of bin occur in zamboni regardless of the order. So bin, nib, ibn, inb are the same for this purpose.

Old Pedant
11-06-2012, 12:27 AM
Because when I asked him if match("zamboni","zoozoozoo" ) should be true he (seemed to have) said "no".

Here's my solution:


<script>
function funkyLetterMatch( test, letters )
{
var s = test.toLowerCase().split("").sort();
var p = letters.toLowerCase().split("").sort();
var x = 0;
for ( var n = 0; n < p.length; ++n )
{
while ( p[n] > s[x] )
{
if ( ++x >= s.length ) { return false; }
}
if ( p[n] < s[x] ) { return false; }
++x;
}
return true;
}

function demo( s, p )
{
document.write( "Pattern " + p + " is in " + s + "? " + funkyLetterMatch( s, p ) + "<br/>" );
}

demo( "zamboni", "aaa" );
demo( "zamboni", "BIN" );
demo( "zAmBoNi", "nab" );
demo( "zamBONI", "cabin" );
</script>


Your "fix" is kind of elegant, though. Just zap the found letter so it can't be found again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum