JavaScript JS Zufallszahlen sortieren

[F]L4SH

Rear Admiral
Registriert
Aug. 2008
Beiträge
5.282
Guten Abend!

Ich brauche dringend mal die Hilfe der erfahrenen Progger hier. Habe ein mittelgroßes Problem mit Javascript.
Habe meinem kleinen Cousin in meiner unendlichen Arroganz (ich war der Meinung ich kann eh alles) versprochen ihm bei einer Info Hausaufgabe über Javascript zu helfen.
Irgendwann vor ein paar Jahren hatte ich das mal. Nun ist mir aber irgendwo mittendrin aufgefallen, dass ich es nicht mehr kann und auch mit SelfHTML kam ich nicht wirklich weiter.

Es geht um einen Lottosimulator. Es sollen 6 Zahlen +SZ,ZZ gezogen werden. Die Zufallsziehung habe ich auch schon mit
Code:
 zahl = Math.floor(Math.random()*49)+1
gemacht. Klappt auch - habe Doppelziehungen ausgeschlossen und das funktioniertauch super.

Das wäre Aufgabe 1.

2. ist aber mein Problem! 2. Besagt, dass man die bei 1. gezogenen Zahlen in eine aufsteigende Reihenfolge bringen.
Nur habe ich null Plan, wie man das machen soll.

Kann mir jemand helfen? Ich verzweifle!

Hier der ganze Code:
Code:
<html>
<head>
<title>Lotto-Simulator</title>

<style type="text/css">
body { font:14px Helvetica }
h1 { font-size:16px; text-decoration:underline;}
input { text-align:center }
.hi1 {background-color: lightgrey }
.hi2 {background-color: coral }
img { border:2px black solid; margin-left: 10px; float:right }
table { border-spacing:5px; font:14px Helvetica }
td, th { border:1px black solid; width:50px; text-align:center }
li {margin-bottom:10px }
span { font-weight:bold;  }
form { margin:0 }
</style>

<script type="text/javascript">
<!--
var zahl = 0;
function ziehen() {
   for (var i = 1; i <= 7; i++)
   document.getElementById("z"+i).firstChild.data=" ";
   document.getElementById("sz").firstChild.data=" ";
   alert("Alles leer?");
   for (var i = 1; i <= 7; i++)
   {
   zahl = Math.floor(Math.random()*49)+1;
     for (var j = 1; j < i; j++)
     if (zahl == document.getElementById("z"+j).firstChild.data) 
     { alert(zahl); zahl = Math.floor(Math.random()*49)+1;j-- }
   document.getElementById("z"+i).firstChild.data = zahl;
   }
   zahl = Math.floor(Math.random()*10);
   document.getElementById("sz").firstChild.data = zahl;
}
//-->
</script>

</head>
<body>
<img src="6aus49.jpg">
<h1>Lotto-Simulator</h1>
<p>Der Computer "denkt" sich 6 + 1 (<span>Z</span>usatz<span>Z</span>ahl) Zahlen zwischen 1 und 49 (keine Zahl darf mehr als einmal vorkommen) sowie eine <span>S</span>uper<span>Z</span>ahl (0-9) aus und zeigt diese
<ol>
<li>in der Reihenfolge der Ziehung und</li>
<li>in geordneter Reihenfolge (Zahl 1 - 6 aufsteigend, Zusatzzahl, Superzahl) an.</li>
</ol>
<br style="clear:both">
<hr>
<form name="formular" action="">
<input type="button" value="Ziehung" onclick="ziehen()">
</form>
<hr>
zu 1.
<table>
<tr>
<td>Zahl 1:</td><td id="z1">*</td>
<td>Zahl 2:</td><td id="z2">*</td>
<td>Zahl 3:</td><td id="z3">*</td>
<td>Zahl 4:</td><td id="z4">*</td>
<td>Zahl 5:</td><td id="z5">*</td>
<td>Zahl 6:</td><td id="z6">*</td>
<th>ZZ:</th><td id="z7">*</td>
<th>SZ:</th><td id="sz">*</td>
</tr>
</table>
<hr>
zu 2.
<table>
<tr>
<td>Zahl 1:</td><td id="gz1">*</td>
<td>Zahl 2:</td><td id="gz2">*</td>
<td>Zahl 3:</td><td id="gz3">*</td>
<td>Zahl 4:</td><td id="gz4">*</td>
<td>Zahl 5:</td><td id="gz5">*</td>
<td>Zahl 6:</td><td id="gz6">*</td>
<th>ZZ:</th><td id="gzz">*</td>
<th>SZ:</th><td id="gsz">*</td>
</tr>
</table>
</body>
</html>

Ich wäre euch für Hilfe super dankbar!
PS: Hoffe, dass ich die Formatierung fürs Forum halbwegs akzeptabel hinbekommen habe.
 
hi,
hm 2 wege; array erstellen und dann per sortieralgorithmus vorgehen (z.b. insertion sort; oder bubble :p)
oder du erstellst dir eine liste und fügst dann entsprechend der Größe der Zahl dieses Element in die Liste ein.
ersteres ist möglicherweise einfacher ;)
bubblesort ist auch für sowas ziemlich einfach, sind ja nur 6 zahlen da brauch man noch keine Aufwandsbetrachtung
 
Setze mich sofort daran. Falls noch Fragen entstehen, stelle ich die dann :)

Danke schonmal
 
Zurück
Oben