Web Analytics Made Easy -
StatCounter why am I returning the index of pos1, and not the replacement?!?! - CodingForum

Announcement

Collapse
No announcement yet.

why am I returning the index of pos1, and not the replacement?!?!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • why am I returning the index of pos1, and not the replacement?!?!

    var input = window.prompt("Enter Text Here", "[[blah|blah]]");
    var pos1 = input.indexOf("[[");

    for(i=pos1;i<=str4.length;i++)
    {
    if(str4.charAt(i) == "[[")
    {
    var pos1 = str4.replace("[[", '<a href="http://en.wikipedia.org/wiki/');
    }

  • #2
    Where is str4 defined?
    Last edited by _Aerospace_Eng_; Oct 7, 2006, 01:34 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

    Comment


    • #3
      it is defined above this code; assume that str4 is defined as from bracket 1 (first [) to bracket last (second ])

      Comment


      • #4
        I'm not going to assume anything. Help us help you. Give us as much information as possible. Just tell us what str4 is. Post the code for that.
        ||||If you are getting paid to do a job, don't ask for help on it!||||

        Comment


        • #5
          haha ok

          <html>
          <head>
          <title>Project.Link</title>
          </head>
          <body>
          <script type= "text/javascript">
          var input = window.prompt("Enter Text Here", "[[blah|blah]]");
          var pos1 = input.indexOf("[[");
          var pos2 = input.indexOf("|");
          var pos3 = input.indexOf("]]");
          var str0 = input.substring(0, pos1);
          var str1 = input.substring(pos1+2, pos2);
          var str2 = input.substring(pos2+1, pos3);
          var str3 = input.substring(pos3+2, input.length);
          var str4 = input.substring(pos1, pos3+1);


          for(i=(pos1+2);i<pos2;i++)
          {
          if(str1.charAt(i) == " ")
          {
          var str1 = str1.replace(" ", "_");
          }
          }

          for(i=pos1;i<=str4.length;i++)
          {
          if(str4.charAt(i) == "[[")
          {
          var pos1 = str4.replace("[[", '<a href="http://en.wikipedia.org/wiki/');
          }
          if(str4.charAt(i) == "|")
          {
          var pos2 = str4.replace("|", '">');
          }
          if(str4.charAt(i) == "]]")
          {
          var pos3 = str4.replace("]]", '</a>');
          }
          }

          var newlink = (pos1);

          document.write(newlink);


          </script>
          </body>
          </html>

          heres the code in its entirety
          at the bottom, newlink keeps giving me the pos1 (index of [[), not the transformation

          Comment


          • #6
            Isn't this the same as this thread?

            http://www.codingforum.net/showthread.php?t=97833

            what exactly are you trying to do?
            The silent one.

            The most dangerous thing in the world is an idea.
            The most dangerous person in the world is the one with an idea.

            Comment


            • #7
              it is ...to an extent

              i mostly have the thing working, except for a few parts;
              i know you gave me an answer, but I don't particularly understand the answer that you gave me.
              I started programming in javascript a little more than a month ago.

              I don't know a whole lot. However, I should be able to do this with just using string manipulations and conditionals/loops. Thats what i'm studying right now.

              Perhaps your method will work.

              Comment


              • #8
                At this line you are comparing the single character at str4 index i against 2 characters,

                if(str4.charAt(i) == "[["){

                so you statement

                pos1 = str4.replace("[[", '<a href="http://en.wikipedia.org/wiki/');

                will not run


                The same will happen at this line

                if(str4.charAt(i) == "]]"){

                With chatAt() you can only compare single characters

                Also you are using the same variable name pos1 as a number and then assigning it a string while still being use in the loop


                for(i=pos1;i<=str4.length;i++){
                if(str4.charAt(i) == "[["){
                pos1 = str4.replace("[[", '<a href="http://en.wikipedia.org/wiki/');
                }

                This only serves to confuse
                The silent one.

                The most dangerous thing in the world is an idea.
                The most dangerous person in the world is the one with an idea.

                Comment


                • #9
                  thanks

                  a million

                  Comment


                  • #10
                    Mr J

                    THANK YOU SO f'IN MUCH...now on the side...how would I loop it if i wanted to?
                    essentially, if there are two urls in the same line, i wanna translate em both into legible html.

                    Heres the code that actually translted the thing:
                    var pos1 = input.indexOf("[[");
                    var pos2 = input.indexOf("|");
                    var pos3 = input.indexOf("]]");
                    var str0 = input.substring(0, pos1);
                    var str1 = input.substring(pos1+2, pos2);
                    var str2 = input.substring(pos2+1, pos3);
                    var str3 = input.substring(pos3+2, input.length);
                    var str4 = input.substring(pos1, pos1+2);
                    var str5 = input.substring(pos2, pos2+1);
                    var str6 = input.substring(pos3, pos3+2);

                    if(input.indexOf("[[") != -1)
                    {
                    var npos1 = str4.replace("[[", '<a href="http://en.wikipedia.org/wiki/');
                    }
                    if(input.indexOf("|") != -1)
                    {
                    var npos2 = str5.replace("|", '">');
                    }
                    if(input.indexOf("]]") != -1)
                    {
                    var npos3 = str6.replace("]]", '</a>');
                    }

                    i'm thinkin you just set the entire statement in a while loop that is like:
                    while(input != -1)

                    So while there is an input around....put the translator on it.

                    I'm assuming there is a flaw in my logic....which there usually is.

                    Comment


                    • #11
                      Here's a couple of examples, the first one relates to your first post and creates a link from the input string

                      PHP Code:
                      <script type"text/javascript">
                      //var input = window.prompt("Enter Text Here", "[[blah|blah]]");

                      input="[[blah|BLAH]]"

                      for(var i=0;i<input.length;i++){ 
                      if(
                      input.charAt(i) == " "){ 
                      input input.replace(" ""_")
                      }
                      }


                      for(var 
                      j=0;j<input.length;j++){ 

                      if(
                      input.charAt(j) == "["){
                      input input.replace("[["'<a href="http://en.wikipedia.org/wiki/')
                      }

                      if(
                      input.charAt(j) == "|"){
                      input input.replace("|""\">")
                      }

                      if(
                      input.charAt(j) == "]"){
                      input input.replace("]]""</a>")
                      }

                      }

                      newlink input

                      alert
                      (newlink)

                      document.write(newlink)

                      </
                      script
                      To create 2 links you would have to include a seperator to define the beginning of the second link.
                      You would then split the string and run the statements twice as per the following example


                      PHP Code:
                      <script type"text/javascript">
                      //var input = window.prompt("Enter Text Here", "[[blah|blah]]");

                      input="[[blah|BLAH]]&[[foo|FOO]]"

                      newLinks=input.split("&")

                      for(var 
                      i=0;i<newLinks.length;i++){

                      for(var 
                      j=0;j<newLinks[i].length;j++){ 
                      if(
                      newLinks[i].charAt(j) == " "){ 
                      newLinks[i] = newLinks[i].replace(" ""_")
                      }
                      }


                      for(var 
                      k=0;k<newLinks[i].length;k++){ 

                      if(
                      newLinks[i].charAt(k) == "["){
                      newLinks[i] = newLinks[i].replace("[["'<a href="http://en.wikipedia.org/wiki/')
                      }

                      if(
                      newLinks[i].charAt(k) == "|"){
                      newLinks[i] = newLinks[i].replace("|""\">")

                      }

                      if(
                      newLinks[i].charAt(k) == "]"){
                      newLinks[i] = newLinks[i].replace("]]""</a>")
                      }
                      }


                      }

                      alert(newLinks)

                      document.write(newLinks[0]+"<br>"+newLinks[1])

                      </
                      script
                      Last edited by Mr J; Oct 7, 2006, 06:57 PM.
                      The silent one.

                      The most dangerous thing in the world is an idea.
                      The most dangerous person in the world is the one with an idea.

                      Comment

                      Working...
                      X