Question

I am making a program that when a button is clicked it makes text appear randomly from an array (i have this part done). What I need help with is making the button able to be pressed over and over again and display something else every time.

input type="button" value="Maths" 
onClick = "document.getElementById('4').style.display='block';">
div id="4" style="display:none;"

script language="Javascript"
sent = new Array (6);
sent [0] = "Don't cry because it's over. Smile because it happened. -Dr.Seuss";
sent [1] = "So many books, so little time. -Frank Zappa";
sent [2] = "In three words I can sum up everything I've learned about life: it goes on. -Robert Frost";
sent [3] = "If you tell the truth, you don't have to remember anything. -Mark Twain";
sent [4] = "To live is the rarest thing in the world. Most people exist, that is all. -Oscar Wilde";
sent [5] = "To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment. - Ralph Waldo Emerson";
x=Math.floor(Math.random()*6);  

    document.write(sent[x])
/script/div
Was it helpful?

Solution

Remove shown elements from the array

sent.splice(x,1);

OTHER TIPS

You should place this in a loop. This will make the code persistent.

while (condition) {
code block to be executed
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top