Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lehrkraefte:blc:informatik:ffprg1-2024:random [2024/03/08 13:08] Ivo Blöchliger |
lehrkraefte:blc:informatik:ffprg1-2024:random [2024/04/05 13:10] (current) Ivo Blöchliger [Anleitung] |
||
---|---|---|---|
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, | ||
} | } | ||
</ | </ | ||
Line 48: | Line 52: | ||
return wort.substring(0, | return wort.substring(0, | ||
} | } | ||
+ | // Test-Code | ||
+ | console.log(`vertauschen(' | ||
+ | console.log(`vertauschen(' | ||
</ | </ | ||
</ | </ | ||
Line 73: | Line 80: | ||
return vertauschen(wort, | return vertauschen(wort, | ||
} | } | ||
+ | // Test Code | ||
+ | console.log(wortwuerfeln(" | ||
</ | </ | ||
</ | </ | ||
Line 91: | Line 100: | ||
<WRAP todo> | <WRAP todo> | ||
- | Die letzte Funktion '' | + | Die letzte Funktion '' |
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, | ||
+ | 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 = ""; |