javascript functions problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akb077
    New Member
    • Nov 2008
    • 1

    javascript functions problem

    I am having problems trying to insert this function into this piece of code below.


    [CODE=javascript]var pages=['www.lboro.ac.u k:degree programmes','ch annel4.co.uk', 'www.bbc.co.uk: sports', 'www.skynews.co m:the best election']
    var w='degree'
    var p=0
    if(pages[p].indexOf(w)<0)
    alert ('not found')
    else
    alert('found')[/CODE]

    i want to use the function find_1(w,p) - to discover if the string w appears in the string pages[p]. Gives an alert of w 'found' or 'not found'. but when i put the function, it doesn't run
    Last edited by gits; Nov 7 '08, 10:35 AM. Reason: added code tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You don't have Java problems, you have Javascript problems; those two languages
    are not the same no matter their name similarity. Do you want me to move your
    question over to the .NET forum where such questions are answered?

    kind regards,

    Jos (moderator)

    Comment

    • karthickkuchanur
      New Member
      • Dec 2007
      • 156

      #3
      whats wrong in it its working fine

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        what do you mean with 'put the function' ... where do you include/call it?

        kind regards

        Comment

        • rnd me
          Recognized Expert Contributor
          • Jun 2007
          • 427

          #5
          i think you need brackets for else.

          thus:

          Code:
          if(pages[p].indexOf(w)<0)
          alert ('not found')
          else
          alert('found')
          should be

          Code:
          if(pages[p].indexOf(w)<0){
            alert ('not found')
          }else{
            alert('found'); 
          }

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            unless there is just one, and only one, statement, the brackets are not strictly required ... but i would even recommend that they are always used since you just have to add them when you want to extend the code later on ... and even use the semicolon to terminate the statements ... its good practice to do so, even when they are not strictly required too :)

            kind regards

            Comment

            • rnd me
              Recognized Expert Contributor
              • Jun 2007
              • 427

              #7
              Originally posted by gits
              unless there is just one, and only one, statement, the brackets are not strictly required ... but i would even recommend that they are always used since you just have to add them when you want to extend the code later on ... and even use the semicolon to terminate the statements ... its good practice to do so, even when they are not strictly required too :)

              kind regards
              touchè.


              thanks, its been a couple months since i learned something new about javascript.
              also gave me another optimization for my code compressor.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                so do you want to 'compress-remove' the brackets, for deployment-versions? quite a nice idea, and certainly worth a look at ... since a lot of brackets and whitespaces could be left out for getting a working script, but its not well readable then ;)

                kind regards

                Comment

                Working...