lehrkraefte:blc:informatik:ffprg1-2024:random

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
lehrkraefte:blc:informatik:ffprg1-2024:random [2024/03/01 12:34]
Ivo Blöchliger [Anleitung]
lehrkraefte:blc:informatik:ffprg1-2024:random [2024/04/05 13:10] (current)
Ivo Blöchliger [Anleitung]
Line 2: Line 2:
 Die JavaScript-Funktion ''Math.random()'' liefert eine Zufallszahl zwischen 0 (inklusive) und 1 (exklusive). Die JavaScript-Funktion ''Math.random()'' liefert eine Zufallszahl zwischen 0 (inklusive) und 1 (exklusive).
  
-Damit lassen sich aus ganzzahlige Zufallszahlen generieren (Funtion und Name sind der entsprechenden Python-Funktion nachempfunden):+Damit lassen sich aus ganzzahlige Zufallszahlen generieren (Funktion und Name sind der entsprechenden Python-Funktion nachempfunden):
 <code javascript> <code javascript>
 // Liefert eine ganzzahlige Zufallszahl von 0 bis n-1 // Liefert eine ganzzahlige Zufallszahl von 0 bis n-1
Line 25: Line 25:
 function randIn(a,b) { function randIn(a,b) {
     return Math.floor(Math.random()*(b-a+1))+a;     return Math.floor(Math.random()*(b-a+1))+a;
 +}
 +// Test-Code
 +for (let i=0; i<20; i++) {
 +  console.log(randIn(11,16));
 } }
 </code> </code>
Line 48: Line 52:
     return wort.substring(0,a)+wort[b]+wort.substring(a+1,b)+wort[a]+wort.substring(b+1,wort.length);     return wort.substring(0,a)+wort[b]+wort.substring(a+1,b)+wort[a]+wort.substring(b+1,wort.length);
 } }
 +// Test-Code
 +console.log(`vertauschen('Buch', 1, 2) liefert ${vertauschen('Buch', 1, 2)} (sollte Bcuh sein)`);
 +console.log(`vertauschen('Reise', 1, 3) liefert ${vertauschen('Reise', 1, 2)} (sollte Rsiee sein)`);
 </code> </code>
 </hidden> </hidden>
Line 73: Line 80:
     return vertauschen(wort, position1, position2);     return vertauschen(wort, position1, position2);
 } }
 +// Test Code
 +console.log(wortwuerfeln("Orange"));
 </code> </code>
 </hidden> </hidden>
Line 91: Line 100:
  
 <WRAP todo> <WRAP todo>
-Die letzte Funktion ''textwuerfeln(text)'' soll Wörter innerhalb eines Texts verwüferln und den neuen Text als Resultat zurückgeben.+Die letzte Funktion ''textwuerfeln(text)'' soll Wörter innerhalb eines Texts verwürfeln und den neuen Text als Resultat zurückgeben.
 Die Funktion soll wie folgt vorgehen: Die Funktion soll wie folgt vorgehen:
  
Line 104: Line 113:
 <hidden Lösungsvorschlag> <hidden Lösungsvorschlag>
 <code javascript> <code javascript>
 +// Ganzzahlige Zufallszahl von a bis und mit b
 +function randIn(a,b) {
 +    return Math.floor(Math.random()*(b-a+1))+a;
 +}
 +
 +function vertauschen(wort, a, b) {
 +    if (a>b) { 
 +        let h = a;
 +        a = b;
 +        b = h;
 +    }
 +    return wort.substring(0,a)+wort[b]+wort.substring(a+1,b)+wort[a]+wort.substring(b+1,wort.length);
 +}
 +
 +// Verwürfelt zwei Buchstaben eines Worts
 +function wortwuerfeln(wort) {
 +    if (wort.length<4) {
 +        return wort;   // Nichts zu tun, Funktion beenden.
 +    }
 +    // Hier ist kein else nötig, weil die Funktion im if beendet wird. => Gute Praxis!
 +    let n = wort.length-2; // Index von vorletzter Stelle
 +    let position1 = randIn(1,n);
 +    let position2 = randIn(1,n);
 +    while (position1==position2) {  // Würfeln bis verschieden...
 +        position2 = randIn(1,n);
 +    }
 +    return vertauschen(wort, position1, position2);
 +}
 +
 +function wortzeichen(z) {
 +    return z.toLowerCase()>='a' && z.toLowerCase()<='z';
 +}
 +
 +
 +
 function textwuerfeln(text) { function textwuerfeln(text) {
     let neu = "";     let neu = "";
  • lehrkraefte/blc/informatik/ffprg1-2024/random.1709292847.txt.gz
  • Last modified: 2024/03/01 12:34
  • by Ivo Blöchliger