From be3e41749cd19c566a6887d2b23886da86ead691 Mon Sep 17 00:00:00 2001 From: Philippe Torrel Date: Fri, 25 Sep 2026 10:48:02 +0200 Subject: [PATCH] --- .../u12_mehrere-dateien-auslesen/index.js | 32 ++++++++++- 02_advanced/uebungen/u13_dns-lookup/index.js | 56 ++++++++++++------- .../uebungen/u13_dns-lookup/package-lock.json | 28 ++++++++++ .../uebungen/u14_avg-scores/index.html | 37 ++++++++++++ .../uebungen/u15_shopping-list/index.html | 21 ++++++- 02_advanced/uebungen/u16_geoquiz/index.html | 18 +++++- .../u17_inventory-modification/index.html | 8 ++- .../u18_3d-array-selection/index.html | 4 +- .../u19_brightness-increase/index.html | 10 +++- .../unterricht/tag15/01_fetch-api/index.html | 0 10 files changed, 182 insertions(+), 32 deletions(-) create mode 100644 02_advanced/uebungen/u13_dns-lookup/package-lock.json create mode 100644 02_advanced/unterricht/tag15/01_fetch-api/index.html diff --git a/02_advanced/uebungen/u12_mehrere-dateien-auslesen/index.js b/02_advanced/uebungen/u12_mehrere-dateien-auslesen/index.js index 3e87003..18fed1c 100644 --- a/02_advanced/uebungen/u12_mehrere-dateien-auslesen/index.js +++ b/02_advanced/uebungen/u12_mehrere-dateien-auslesen/index.js @@ -1,6 +1,34 @@ -// const getFileBy = (path, encoding = 'utf-8') => { +import fs from 'node:fs'; -// }; +const getFileBy = (path = '', encoding = 'utf-8') => { + return new Promise((resolve, reject) => { + fs.readFile(path, encoding, (error, data) => { + if (error) { + reject(error); + } else { + resolve(data); + } + }); + }); +}; + +Promise.all([ + getFileBy('./data/file01.txt'), + getFileBy('./data/file02.txt'), + getFileBy('./data/file03.txt'), + // getFileBy('./data/file_03.txt'), + getFileBy('./data/file04.txt'), + getFileBy('./data/file05.txt'), + getFileBy('./data/file06.txt'), +]) + .then((data) => + data.forEach((data) => { + console.log(data); + }), + ) + .catch((error) => { + console.log(error); + }); /* diff --git a/02_advanced/uebungen/u13_dns-lookup/index.js b/02_advanced/uebungen/u13_dns-lookup/index.js index 76bb75d..79bbe5e 100644 --- a/02_advanced/uebungen/u13_dns-lookup/index.js +++ b/02_advanced/uebungen/u13_dns-lookup/index.js @@ -1,24 +1,43 @@ -// TODO: imports +import dns from 'node:dns'; +import color from 'chalk'; +import util from 'node:util'; const IP_V = 4; // we use IP protocol version 4 const URL = 'google.de'; -// TODO: your modifications - // 1. -// const getIpBy = () => {}; +const getIpBy = (url, ip = 4) => { + return new Promise((resolve, reject) => { + dns.lookup(url, ip, (error, data) => { + if (error) { + reject(error); + } else { + resolve({ address: data, family: ip }); + } + }); + }); +}; // 2. // Promises Unterobjekt von Node -// const getPromisesIpBy = () => {}; +const getPromisesIpBy = (url, ip = 4) => { + return dns.promises.lookup(url, ip); +}; // 3. // Promisify Helferfunktion -// const getPromisifyIpBy = - +const getPromisifyIpBy = util.promisify(dns.lookup); // 4. // Async await -// const getAsyncIpBy = +const getAsyncIpBy = async (url, ip) => { + try { + const result = await dns.promises.lookup(url, ip); + return result; + } catch (err) { + console.log(err); + throw new Error(err); + } +}; // Test Cases: // ============================= @@ -55,23 +74,20 @@ getAsyncIpBy(URL, IP_V) }); /* Übung 13: Das Telefonbuch des Internets - -Vielleicht hast du einmal die Webseite www.nodejs.com besucht? Dann hat dein Computer die so genannte IP-Adresse des Servers www.nodejs.com herausgefunden. - + +Vielleicht hast du einmal die Webseite www.nodejs.com besucht? Dann hat dein Computer die so genannte IP-Adresse des Servers www.nodejs.org herausgefunden. + Ähnlich wie ein Telefonbuch ordnet das Domain Name System (DNS) jedem Server (Host) eine eindeutige IP-Adresse zu, unter der er über das Internet erreichbar ist. - + Die Node.js-API bietet eine Funktion, um Namen im DNS nachzuschlagen: dns.lookup(…). - + Die Übung besteht nun darin, obiges Beispiel so umzuschreiben, dass der Aufruf von dns.lookup(…) in eine Wrapper-Funkion verpackt wird. - + Es sollen drei verschiedene Lösungen erstellt werden: - + 1. Ausführliche Variante mit eigenem Promise-Objekt als Ummantelung - -2. Kurze Variante mit Promises-Unterobjekt (dns.promises) - +2. Kurze Variante mit Promises-Unterobjekt (dns.promises) 3. Promisify-Variante mit util.promisify(…) - 4. Async await Variante - + */ diff --git a/02_advanced/uebungen/u13_dns-lookup/package-lock.json b/02_advanced/uebungen/u13_dns-lookup/package-lock.json new file mode 100644 index 0000000..6ec7862 --- /dev/null +++ b/02_advanced/uebungen/u13_dns-lookup/package-lock.json @@ -0,0 +1,28 @@ +{ + "name": "u13_dns-lookup", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "u13_dns-lookup", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "chalk": "^6.0.0" + } + }, + "node_modules/chalk": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-6.0.0.tgz", + "integrity": "sha512-2uNTXIuTTxk7ciZgAU1BQcgnchcG0xXnrs6jzkQfj9SsRa9M2s5zE8WT96hS6KmG4MzWHSrvH43DF1m4XRkrFg==", + "license": "MIT", + "engines": { + "node": ">=22" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + } + } +} diff --git a/02_advanced/uebungen/u14_avg-scores/index.html b/02_advanced/uebungen/u14_avg-scores/index.html index db9e9c2..0f234ed 100644 --- a/02_advanced/uebungen/u14_avg-scores/index.html +++ b/02_advanced/uebungen/u14_avg-scores/index.html @@ -44,6 +44,43 @@ ['Clara', 76, 95, 89, 91], ['David', 82, 79, 91, 85], ]; + + const calculateAvgGrades = () => { + const averages = []; + + for (let row = 1; row < gradesTable.length; row++) { + let sum = 0; + + for (let column = 1; column < gradesTable[row].length; column++) { + sum += gradesTable[row][column]; + } + + const numberOfSubjects = gradesTable[row].length - 1; + averages.push(Number((sum / numberOfSubjects).toFixed(2))); + } + + return averages; + }; + + const calculateAvgSubjects = () => { + const averages = []; + + for (let column = 1; column < gradesTable[0].length; column++) { + let sum = 0; + + for (let row = 1; row < gradesTable.length; row++) { + sum += gradesTable[row][column]; + } + + const numberOfStudents = gradesTable.length - 1; + averages.push(Number((sum / numberOfStudents).toFixed(2))); + } + + return averages; + }; + + console.log(calculateAvgGrades()); // => [85.75, 85.25, 87.75, 84.25] + console.log(calculateAvgSubjects()); // => [83.25, 88.5, 85.5, 85.75] diff --git a/02_advanced/uebungen/u15_shopping-list/index.html b/02_advanced/uebungen/u15_shopping-list/index.html index 9888d2f..e971340 100644 --- a/02_advanced/uebungen/u15_shopping-list/index.html +++ b/02_advanced/uebungen/u15_shopping-list/index.html @@ -1,4 +1,4 @@ - + @@ -32,7 +32,24 @@ ['Dairy', 'Milk', 'Cheese', 'Yogurt'], ]; - // Your code here + shoppingList[0][2] = 'Grapes'; // Replace 'Bananas' with 'Grapes' + shoppingList[1][2] = 'Tomatoes'; // Replace 'Broccoli' with 'Tomatoes' + shoppingList[2][2] = 'Butter'; // Replace 'Cheese' with 'Butter' + + // let replaceAr = ['Grapes', 'Tomatoes', 'Butter']; + + // const results = shoppingList.map((list, idx) => { + // return list.toSpliced(2, 1, replaceAr[idx]); + // }); + + // console.log(results); + + // Expected outcome: + // [ + // ['Fruits', 'Apples', 'Grapes', 'Oranges'], + // ['Vegetables', 'Carrots', 'Tomatoes', 'Spinach'], + // ['Dairy', 'Milk', 'Butter', 'Yogurt'], + // ] diff --git a/02_advanced/uebungen/u16_geoquiz/index.html b/02_advanced/uebungen/u16_geoquiz/index.html index 0f72453..76890d3 100644 --- a/02_advanced/uebungen/u16_geoquiz/index.html +++ b/02_advanced/uebungen/u16_geoquiz/index.html @@ -1,4 +1,4 @@ - + @@ -36,7 +36,21 @@ ['Russia', 'Moscow'], ]; - const getCapitalOf = () => {}; + const getCapitalOf = (country) => { + return countriesWithCapital.find((countryWithCapital) => countryWithCapital[0] === country)[1]; + }; + + // const capitalOf = (country) => { + // const capitalIndex = 1; + // const countryIndex = 0; + // return countriesWithCapital.find((countryWithCapital) => countryWithCapital[countryIndex] === country)[ + // capitalIndex + // ]; + // }; + + console.log('Switzerland:', getCapitalOf('Switzerland')); + console.log('Germany:', getCapitalOf('Germany')); + console.log('Russia:', getCapitalOf('Russia')); diff --git a/02_advanced/uebungen/u17_inventory-modification/index.html b/02_advanced/uebungen/u17_inventory-modification/index.html index b1db331..879cd89 100644 --- a/02_advanced/uebungen/u17_inventory-modification/index.html +++ b/02_advanced/uebungen/u17_inventory-modification/index.html @@ -1,4 +1,4 @@ - + @@ -39,7 +39,11 @@ ], ]; - // Your code here + // Add the Books category with its products. + inventory.push([ + ['Shelf 1', 'Novels', 40], + ['Shelf 2', 'Non-fiction', 35], + ]); console.log(inventory); /* Expected outcome: diff --git a/02_advanced/uebungen/u18_3d-array-selection/index.html b/02_advanced/uebungen/u18_3d-array-selection/index.html index a655911..38bc5dd 100644 --- a/02_advanced/uebungen/u18_3d-array-selection/index.html +++ b/02_advanced/uebungen/u18_3d-array-selection/index.html @@ -1,4 +1,4 @@ - + @@ -44,7 +44,7 @@ ], ]; - // Your code here + inventory[1][1][2] = 25; console.log(inventory); /* diff --git a/02_advanced/uebungen/u19_brightness-increase/index.html b/02_advanced/uebungen/u19_brightness-increase/index.html index 0160b3f..34be890 100644 --- a/02_advanced/uebungen/u19_brightness-increase/index.html +++ b/02_advanced/uebungen/u19_brightness-increase/index.html @@ -1,4 +1,4 @@ - + @@ -41,7 +41,13 @@ ]; const increaseBrightness = (image, value) => { - // Your code here + return image.map((row) => + // + row.map((pixel) => + // + pixel.map((idx) => Math.min(idx + value, 255)), + ), + ); }; const newImage = increaseBrightness(image, 30); diff --git a/02_advanced/unterricht/tag15/01_fetch-api/index.html b/02_advanced/unterricht/tag15/01_fetch-api/index.html new file mode 100644 index 0000000..e69de29