...

View Full Version : need help producing this weird function



jimbob
11-06-2012, 08:14 PM
ok so i am creating this function mx(pages,pattern)

ok i got an array of pages

pages=pages=[
"welcome to England.",
"Barcalona is a good holiday.",
" Madrid is yet another fine city."
];

what i have to do is make return an array of indexes of each page in pages that match pattern regardless of the order of the pattern as long as the letters exist in the page
e.g alert(mx(pages,"ae")) returns 0 and 1

i have no idea how to start this problem let alone solve anyone got any ideas

this is what i think

var patternlc2 = pattern.toLowerCase();
var pages1 = pages.toLowerCase();
for (var i = 0, length = patternlc2.length; i < length; i++)
var char= patternlc2.charAt(i)
{

Philip M
11-06-2012, 08:21 PM
Oh no! Not again! ;)

We have exhausted this topic quite recently at

http://www.codingforums.com/showthread.php?t=281061

The OP in that thread concealed the fact that this was homework. :(

No more, please!

You do not have an array of "pages". It is an array of sentences. "Pages" is not a very good name.
You declare an array with


var pages=["welcome to England.","Barcalona is a good holiday."," Madrid is yet another fine city."];

The cheese in the mousetrap is always free.

jimbob
11-06-2012, 08:40 PM
Oh no! Not again! ;)

We have exhausted this topic quite recently at

http://www.codingforums.com/showthread.php?t=281061

The OP in that thread concealed the fact that this was homework. :(

No more, please!

You do not have an array of "pages". It is an array of sentences. "Pages" is not a very good name.
You declare an array with


var pages=["welcome to England.","Barcalona is a good holiday."," Madrid is yet another fine city."];

The cheese in the mousetrap is always free.

thanks for the link very helpful however how would i go about linking the words to the array of sentences

Philip M
11-06-2012, 09:17 PM
thanks for the link very helpful however how would i go about linking the words to the array of sentences

Surely it is obvious? Using the code in Post#20 and corrected at Post#24


match(pages[0], "CAFE");
match(pages[1], "something");


That is match the string with the pattern.

jimbob
11-06-2012, 09:43 PM
Surely it is obvious? Using the code in Post#20 and corrected at Post#24


match(pages[0], "CAFE");
match(pages[1], "something");


That is match the string with the pattern.

For example if i enter alert(gdsgsd(pages,"hello")

This would return me an answer of 0,1

Old Pedant
11-06-2012, 10:03 PM
No, actually the code in that thread (any of it) only returns true or false depending on if a mathc is found or not.

And, also, the name of the function he/we used was just match. (Well...I used funkyLetterMatch which is still a better name....but the best name might be inaneHomeworkMatch or maybe "insane" instead of "inane".)

If the homework assignment was to produce the array you described, then maybe "Nervous" (the person who started that thread) will get a bad grade, as he deserves for not saying this is homework.

Since we have done the hard work for you, it seems fair that you should at least figure out how to produce the required array.

See http://www.codingforums.com/rules.htm and especially rule number 1.5.

Philip M
11-07-2012, 07:53 AM
For example if i enter alert(gdsgsd(pages,"hello")

This would return me an answer of 0,1

I am totaly baffled! What are 0 and 1 supposed to represent? False and true? How can anything be both?

The syntax is

alert (nameOfFunction("StringToBeExamined", "CharactersToBeMatched"))

A literal string (such as "Madrid is yet another fine city") is enclosed in quotes, a variable such as pages[2] (the value of an array element) is not.

Old Pedant
11-07-2012, 08:16 PM
I think he means the array is supposed to indicate *WHICH* elements of the pages array matched.

So he could still use the match( ) as we gave it in the other thread, just in a loop testing each of the pages array elements and the recording the ones that returned true.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum