Jump to content

Display multiple random numbers using Array


inktherapy

Recommended Posts

Hi Guys,I'm wondering if you could find a work around for this script, you see I want to display multiple random numbersusing Arrays. can you look at my sampled script, thanks...

<script type="text/javascript">var x = Math.random();var a = new Array();var b;for (i=0; i<=4; i++) {b = confirm("Do you still want random math?");if (b==true) {a[x]=x;} else {alert("Okay thank you come again");}}document.write(x + "<br /> "  );document.write(a[0] + "<br />");document.write(a[1] + "<br />");document.write(a[2] + "<br />");document.write(a[3] + "<br />");</script>

Link to comment
Share on other sites

I got a little carried away here, but one thing led to another. Mostly, I couldn't stand to see all those document.write() statements. Then I needed a place for output. Then I needed to style that place . . .But you can focus just on the script, which is where the important changes are. I should say for the record that an array really isn't needed for this functionality. We could output the results as soon as we generate them. But I guessed that you're partly tutoring yourself in arrays right now, so I left that part in, only slightly modified.If you study the rest of the code, you can learn a few other basics. (Assuming you need them.) FWIW, the page validates as XHTML strict.

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">	<head>		<title></title>		<style type="text/css">			* {				margin: 0;				padding: 0;			}			body, html {text-align: center}			div.container {				width: 780px;				margin: auto;				text-align: left;			}			h1 {				text-align: center			}			div#output {				background-color: #bbffbb;				border: solid 1px #000000;				font-family: Verdana, Arial, san-serif;				height: 6em;				padding: .5em;				margin-bottom: 1em;			}		</style>		<script type="text/javascript">			function output_random () {				var a = new Array();				var str = "";				for (i = 0; i <= 4; i++) {					a[i] = Math.random();				}						// THIS NEXT PART COULD BE TUCKED INTO THE FOR LOOP ABOVE						str += a[0] + "<br />";				str += a[1] + "<br />";				str += a[2] + "<br />";				str += a[3] + "<br />";		// END REDUNDANT SECTION						document.getElementById("output").innerHTML = str;			}			function init () {				document.getElementById("mybutton").onclick = output_random;			}			window.onload = init;		</script>	</head>	<body>		<div class="container">			<h1>Random Number Generator</h1>			<div id="output"></div>			<button type="button" id="mybutton">Randomize</button>		</div>	</body></html>

Link to comment
Share on other sites

Good work, Dad. The main problem with the original script was just that you were never resetting x, you were always assigning the same random number to each element in the array. That's one of the things Dad's code changes.a = Math.random();

Link to comment
Share on other sites

Hi Dad,Thanks for the help, you're right I'm actually learning Array right now. Your version is what I have in mind but unfortunately the outcome were not. Well I'm going to study this and save it for future reference.Many thanks.To justsomeguy,Thanks for the insights, you were right my method is somewhat crap, I realise that i have to put the Array in a for loop so it can generate different random numbers not same. hehehe anyways Dad's code fix it for me. Have a nice day.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...