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
lehrkraefte:blc:informatik:ffprg1-2024:random [2024/03/22 13:29]
Ivo Blöchliger [Anleitung]
lehrkraefte:blc:informatik:ffprg1-2024:random [2024/04/05 13:10] (current)
Ivo Blöchliger [Anleitung]
Line 113: 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.txt
  • Last modified: 2024/04/05 13:10
  • by Ivo Blöchliger