PDA

View Full Version : why am I returning the index of pos1, and not the replacement?!?!


thenextbesthang
10-07-2006, 04:55 PM
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/');
}

_Aerospace_Eng_
10-07-2006, 05:30 PM
Where is str4 defined?

thenextbesthang
10-07-2006, 05:38 PM
it is defined above this code; assume that str4 is defined as from bracket 1 (first [) to bracket last (second ])

_Aerospace_Eng_
10-07-2006, 05:39 PM
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.

thenextbesthang
10-07-2006, 05:47 PM
<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

Mr J
10-07-2006, 05:49 PM
Isn't this the same as this thread?

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

what exactly are you trying to do?

thenextbesthang
10-07-2006, 05:53 PM
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.

Mr J
10-07-2006, 07:19 PM
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

thenextbesthang
10-07-2006, 07:50 PM
a million

thenextbesthang
10-07-2006, 08:38 PM
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.

Mr J
10-07-2006, 10:53 PM
Here's a couple of examples, the first one relates to your first post and creates a link from the input string

<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


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