Differences
This shows you the differences between two versions of the page.
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, | ||
+ | if (a>b) { | ||
+ | let h = a; | ||
+ | a = b; | ||
+ | b = h; | ||
+ | } | ||
+ | return wort.substring(0, | ||
+ | } | ||
+ | |||
+ | // Verwürfelt zwei Buchstaben eines Worts | ||
+ | function wortwuerfeln(wort) { | ||
+ | if (wort.length< | ||
+ | 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; | ||
+ | let position1 = randIn(1, | ||
+ | let position2 = randIn(1, | ||
+ | while (position1==position2) { // Würfeln bis verschieden... | ||
+ | position2 = randIn(1, | ||
+ | } | ||
+ | return vertauschen(wort, | ||
+ | } | ||
+ | |||
+ | function wortzeichen(z) { | ||
+ | return z.toLowerCase()> | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
function textwuerfeln(text) { | function textwuerfeln(text) { | ||
let neu = ""; | let neu = ""; |