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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    need help producing this weird function

    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
    Code:
     var patternlc2 = pattern.toLowerCase();
        var pages1 = pages.toLowerCase();
        for (var i = 0, length = patternlc2.length; i < length; i++) 
        var char= patternlc2.charAt(i)
     {

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,908
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Oh no! Not again!

    We have exhausted this topic quite recently at

    Need help explaining what the following function is doing

    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

    Code:
    var pages=["welcome to England.","Barcalona is a good holiday."," Madrid is yet another fine city."];
    The cheese in the mousetrap is always free.
    Last edited by Philip M; 11-06-2012 at 08:28 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    jimbob (11-06-2012)

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Oh no! Not again!

    We have exhausted this topic quite recently at

    Need help explaining what the following function is doing

    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

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

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,908
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by jimbob View Post
    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

    Code:
    match(pages[0], "CAFE");
    match(pages[1], "something");
    That is match the string with the pattern.
    Last edited by Philip M; 11-06-2012 at 09:20 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Surely it is obvious? Using the code in Post#20 and corrected at Post#24

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

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,123
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.
    Last edited by Old Pedant; 11-06-2012 at 10:06 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,908
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by jimbob View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,123
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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