1

Here's the code:

<SCRIPT LANGUAGE="Javascript"><!--

function text() {
};

text = new text();
number = 0;

// textArray        
text[number++] = "I read the other day some verses written by an eminent painter which were original and not conventional."         
text[number++] = "The soul always hears an admonition in such lines, let the subject be what it may."        
text[number++] = "The sentiment they instil is of more value than any thought they may contain."        
text[number++] = "To believe your own thought, to believe that what is in your private heart is true for all men, that is genius." 

increment = Math.floor(Math.random() * number);

document.write(text[increment]);

//--></SCRIPT>

Specifically what I'd like the words:

true for you

from the fourth text in the array to link to some external website.

The <a> tag doesn't seem to work for me. Neither does the string method. Of course, I am a total lunkhead.

War10ck
  • 11,732
  • 7
  • 38
  • 50
szhamilton
  • 11
  • 2
  • 1
    You have a syntax error (a quote that wasn't escaped). If that's the _HTML_, it also isn't close-quoted or close-tagged. You could have written an _Array literal_ instead of creating a custom constructor here. Lastly, [`document.write`](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) is a bad idea. – Paul S. Apr 14 '14 at 18:42
  • It's the portion of the HTML I'm working with, which I've borrowed from Javascript Source. I assume the code is close-quoted/close-tagged, though I'm too novice to know for sure. I also don't know what "array literal" nor "document.write" are, or what they do. (Lunkhead, you see). Are these all things that make my original question impossible to answer? – szhamilton Apr 14 '14 at 18:52
  • You have to start somewhere.. – Paul S. Apr 14 '14 at 18:56

2 Answers2

0

I guess your problem is with the Quotes you insert when you use a tag, You should use the quotes in proper sense like below:

text[number++] = "I read the other day some verses written by an eminent painter which were original and not conventional"         
text[number++] = "The soul always hears an admonition in such lines, let the subject be what it may."        
text[number++] = "The sentiment they instil is of more value than any thought they may contain."
text[number++] = "To believe your own thought, to believe that what is in your private heart is <a href='javascript:void(0);'>true for </a> all men, that is genius." 

http://jsbin.com/luyod/2/

defau1t
  • 10,373
  • 2
  • 32
  • 45
  • I'm not interested in having the entire quote link to some other site, just a few words. Specifically, I'd like the words "true for you" from the fourth quote to link to an external site. – szhamilton Apr 14 '14 at 19:01
0

Let's go through this step by step,

  1. Create a new variable called text, and let it be an Array.

    var text = [];
    

    The [] here is an Array literal.

  2. Populate the Array text with your Strings

    text.push("I read the other day some verses written by an eminent painter which were original and not conventional.");
    text.push("The soul always hears an admonition in such lines, let the subject be what it may.");
    text.push("The sentiment they instil is of more value than any thought they may contain.");
    text.push("To believe your own thought, to believe that what is in your private heart is true for all men, that is genius.";
    

    Here, someArray.push is a method which adds a new item to the Array someArray. Each new item gets it's own index and indices start from 0.

  3. Choose a random index from the Array text and assign it to a new variable, i

    var i = Math.floor(Math.random() * text.length);
    

    In JavaScript, the method Math.random returns a value strictly less than 1 but greater than or equal to 0. This means we can multiply the value up by the number of items (or length) of an Array, then round down (or floor) this number to choose a random index from the Array.

  4. In the String at index i of text, replace any occurance of "true for you" with your link. Let's give this the variable name str so we can refer back to it.

    var str = text[i].replace(/true for you/g, "<a href=\"http://stackoverflow.com\">true for you</a>");
    

    Here, /true for you/g is a regular expression or RegExp. The g at the end stands for global and means "keep doing this until you reach the end".

  5. Write this new String (that we called str) to your #document in your prefered way. As we can't see anything relating to your HTML markup, I'll keep your document.write, but this is considered bad practice so alternatives may be something you'd want to learn about in future.

    document.write(str);
    
Community
  • 1
  • 1
Paul S.
  • 58,277
  • 8
  • 106
  • 120