Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| de:modul:m288:learningunits:lu01:aufgaben:11 [2026/09/03 10:37] – angelegt vdemir | de:modul:m288:learningunits:lu01:aufgaben:11 [2026/09/03 10:43] (aktuell) – vdemir | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| ====== LU01.A11 - JS-Codevorlage erstellen====== | ====== LU01.A11 - JS-Codevorlage erstellen====== | ||
| - | ===== Lerziele | + | ===== Lernziele |
| * JavaScript-Code nach BestPractise kommentieren können. | * JavaScript-Code nach BestPractise kommentieren können. | ||
| * Vorlage für BestPractise-Kommentaren erstellen. | * Vorlage für BestPractise-Kommentaren erstellen. | ||
| Zeile 9: | Zeile 9: | ||
| * Hilfsmittel: | * Hilfsmittel: | ||
| * Theorie-Dossier: | * Theorie-Dossier: | ||
| - | * Zeit: 20 Minuten | + | * Zeit: 10 Minuten |
| * Erwartetes Resultat: | * Erwartetes Resultat: | ||
| - | | + | |
| ===== Ausgangslage ===== | ===== Ausgangslage ===== | ||
| - | Nachdem wir nun eigenen JS-Code kommentieren können, wollen wir dazu übergehen fremden Code zu lesen und zu kommentieren | + | Incode-Kommanmtare sind sehr wichtig für das schnelle Einarbeiten |
| ===== Aufgabe ===== | ===== Aufgabe ===== | ||
| - | Kommentieren | + | Erstellen |
| - | | + | |
| * Autor, Datum | * Autor, Datum | ||
| - | * Zielsetzung des Files (wozu braucht es diese Datei?) | + | * Zielsetzung des Files (Wozu braucht es diese Datei?) |
| - | + | * **Methodenheader/Funktionskopf** | |
| - | + | * Autor, Datum | |
| - | **Vorlage: Grundrechenarten · JS** | + | * Zielsetzung |
| - | + | ||
| - | <code javascript> | + | |
| - | /** | + | |
| - | * ===================================================================== | + | |
| - | | + | |
| - | * ===================================================================== | + | |
| - | * | + | |
| - | | + | |
| - | * | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | * | + | |
| - | | + | |
| - | * | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | * | + | |
| - | | + | |
| - | | + | |
| - | * | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | * | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | * ===================================================================== | + | |
| - | */ | + | |
| - | + | ||
| - | "use strict"; | + | |
| - | + | ||
| - | // ===================================================================== | + | |
| - | // TEIL 1 - Zwei Zahlen, vier Rechenarten | + | |
| - | // ===================================================================== | + | |
| - | + | ||
| - | const a = 12; | + | |
| - | const b = 5; | + | |
| - | + | ||
| - | // --- AUFGABE 1 ------------------------------------------------------- | + | |
| - | // TODO: Erklaere, was die beiden Zeilen oben bewirken. Gehe dabei auf | + | |
| - | // das Schluesselwort " | + | |
| - | // | + | |
| - | // Antwort: | + | |
| - | // | + | |
| - | // | + | |
| - | + | ||
| - | function addieren(x, y) { | + | |
| - | return x + y; | + | |
| - | } | + | |
| - | + | ||
| - | function subtrahieren(x, | + | |
| - | return x - y; | + | |
| - | } | + | |
| - | + | ||
| - | function multiplizieren(x, | + | |
| - | return x * y; | + | |
| - | } | + | |
| - | + | ||
| - | // --- AUFGABE 2 ------------------------------------------------------- | + | |
| - | // TODO: Beschreibe den Aufbau einer Funktion | + | |
| - | // " | + | |
| - | // Antwort: | + | |
| - | // | + | |
| - | // | + | |
| - | + | ||
| - | function dividieren(x, y) { | + | |
| - | if (y === 0) { | + | |
| - | return " | + | |
| - | } | + | |
| - | return x / y; | + | |
| - | } | + | |
| - | + | ||
| - | // --- AUFGABE 3 ------------------------------------------------------- | + | |
| - | // TODO: Warum enthaelt ausgerechnet die Funktion " | + | |
| - | // | + | |
| - | // Was wuerde JavaScript ohne diese Abfrage bei 12 / 0 ausgeben? | + | |
| - | // | + | |
| - | // Antwort: | + | |
| - | // | + | |
| - | // | + | |
| - | + | ||
| - | console.log(" | + | |
| - | console.log(`${a} + ${b} = ${addieren(a, | + | |
| - | console.log(`${a} - ${b} = ${subtrahieren(a, | + | |
| - | console.log(`${a} * ${b} = ${multiplizieren(a, | + | |
| - | console.log(`${a} / ${b} = ${dividieren(a, | + | |
| - | console.log(`${a} / 0 = ${dividieren(a, | + | |
| - | + | ||
| - | // --- AUFGABE 4 ------------------------------------------------------- | + | |
| - | // TODO: In den console.log-Zeilen stehen Backticks (`) statt | + | |
| - | // | + | |
| - | // | + | |
| - | // " | + | |
| - | // Antwort: | + | |
| - | // | + | |
| - | // | + | |
| - | + | ||
| - | // ===================================================================== | + | |
| - | // TEIL 2 - Ganzzahldivision und Rest | + | |
| - | // ===================================================================== | + | |
| - | + | ||
| - | const zaehler = 17; | + | |
| - | const nenner = 5; | + | |
| - | + | ||
| - | const ganzeTeile = Math.floor(zaehler / nenner); | + | |
| - | const rest = zaehler % nenner; | + | |
| - | + | ||
| - | console.log(" | + | |
| - | console.log(`${zaehler} : ${nenner} = ${ganzeTeile} Rest ${rest}`); | + | |
| - | + | ||
| - | // --- AUFGABE 5 ------------------------------------------------------- | + | |
| - | // TODO: Erklaere die beiden Operatoren in den Zeilen oben: | + | |
| - | // a) Was macht Math.floor()? | + | |
| - | // b) Was macht der Operator % (Modulo)? | + | |
| - | // Nenne zu b) ein praktisches Beispiel aus dem Alltag der | + | |
| - | // | + | |
| - | // Antwort: | + | |
| - | // | + | |
| - | // | + | |
| - | + | ||
| - | const zahl = 42; | + | |
| - | const istGerade = zahl % 2 === 0; | + | |
| - | console.log(`Ist ${zahl} gerade? ${istGerade}`); | + | |
| - | + | ||
| - | // --- AUFGABE 6 ------------------------------------------------------- | + | |
| - | // TODO: Die Zeile "const istGerade = zahl % 2 === 0;" enthaelt zwei | + | |
| - | // | + | |
| - | // | + | |
| - | // Antwort: | + | |
| - | // | + | |
| - | // | + | |
| - | + | ||
| - | // ===================================================================== | + | |
| - | // TEIL 3 - Rangfolge der Operatoren (Punkt vor Strich) | + | |
| - | // ===================================================================== | + | |
| - | + | ||
| - | const ohneKlammern = 2 + 3 * 4; | + | |
| - | const mitKlammern = (2 + 3) * 4; | + | |
| - | + | ||
| - | console.log(" | + | |
| - | console.log(`2 + 3 * 4 = ${ohneKlammern}`); | + | |
| - | console.log(`(2 + 3) * 4 = ${mitKlammern}`); | + | |
| - | + | ||
| - | // --- AUFGABE 7 ------------------------------------------------------- | + | |
| - | // TODO: Beide Zeilen enthalten dieselben Zahlen und dieselben | + | |
| - | // | + | |
| - | // | + | |
| - | // | + | |
| - | // Antwort: | + | |
| - | // | + | |
| - | // | + | |
| - | + | ||
| - | // ===================================================================== | + | |
| - | // TEIL 4 - Wenn Kommazahlen ungenau werden | + | |
| - | // ===================================================================== | + | |
| - | + | ||
| - | const summe = 0.1 + 0.2; | + | |
| - | + | ||
| - | console.log(" | + | |
| - | console.log(`0.1 + 0.2 = ${summe}`); | + | |
| - | console.log(`Ist 0.1 + 0.2 === 0.3 ? ${summe === 0.3}`); | + | |
| - | console.log(`Gerundet auf 2 Stellen: ${summe.toFixed(2)}`); | + | |
| - | + | ||
| - | // --- AUFGABE 8 ------------------------------------------------------- | + | |
| - | // TODO: Das Ergebnis von 0.1 + 0.2 ueberrascht. Notiere, was in der | + | |
| - | // | + | |
| - | // | + | |
| - | // Wie hilft toFixed(2) weiter - und was ist dabei der Haken? | + | |
| - | // Antwort: | + | |
| - | // | + | |
| - | // | + | |
| - | + | ||
| - | // ===================================================================== | + | |
| - | // TEIL 5 - Eine Rechnung, viele Male: die Schleife | + | |
| - | // ===================================================================== | + | |
| - | + | ||
| - | const reihe = 7; | + | |
| - | + | ||
| - | console.log(`\n=== TEIL 5: Einmaleins der ${reihe} ===`); | + | |
| - | for (let i = 1; i <= 10; i++) { | + | |
| - | console.log(`${reihe} * ${i} = ${multiplizieren(reihe, | + | |
| - | } | + | |
| - | + | ||
| - | // --- AUFGABE 9 ------------------------------------------------------- | + | |
| - | // TODO: Zerlege den Kopf der for-Schleife in seine drei Bestandteile | + | |
| - | // | + | |
| - | // Wie oft wird der Rumpf der Schleife ausgefuehrt? | + | |
| - | // Antwort: | + | |
| - | // | + | |
| - | // | + | |
| - | + | ||
| - | // ===================================================================== | + | |
| - | // TEIL 6 - Eingabe von aussen | + | |
| - | // ===================================================================== | + | |
| - | + | ||
| - | const eingabe = process.argv[2]; | + | |
| - | const eingabeZahl = Number(eingabe); | + | |
| - | + | ||
| - | console.log(" | + | |
| - | if (eingabe === undefined) { | + | |
| - | console.log(" | + | |
| - | } else if (Number.isNaN(eingabeZahl)) { | + | |
| - | console.log(`" | + | |
| - | } else { | + | |
| - | console.log(`Deine Zahl: ${eingabeZahl}`); | + | |
| - | console.log(`Verdoppelt: | + | |
| - | console.log(`Halbiert: | + | |
| - | console.log(`Um 10 vermindert: | + | |
| - | } | + | |
| - | + | ||
| - | // --- AUFGABE 10 ------------------------------------------------------ | + | |
| - | // TODO: Alles, was ueber die Kommandozeile hereinkommt, | + | |
| - | // Text (String). Erklaere, warum Number(eingabe) noetig ist. | + | |
| - | // Was waere das Ergebnis von " | + | |
| - | // und was von " | + | |
| - | // Antwort: | + | |
| - | // | + | |
| - | // | + | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | </ | + | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | |||
| ===== Lösungen ===== | ===== Lösungen ===== | ||
| - | [[de: | + | [[de: |
| ---- | ---- | ||
| [[https:// | [[https:// | ||