This commit is contained in:
Philippe Torrel
2026-09-24 10:25:47 +02:00
parent db1626eb30
commit 6887814561
7 changed files with 174 additions and 3 deletions
-1
View File
@@ -201,7 +201,6 @@ Abschlussquiz - JS Grundlagen
- Promises
- Callback-Hell
-
- Promise Aufbau
- Promise in Funktionen
- File System von Node
@@ -16,7 +16,8 @@
</h2>
<p>
Schreibe eine rekursive Funktion in JavaScript, die die Summe aller Zahlen in einem verschachtelten Array
berechnet. Das Array kann beliebig tief verschachtelt sein und enthält nur Zahlen oder weitere Arrays.
berechnet. Das Array kann beliebig tief verschachtelt sein und sollte nur Zahlen oder weitere Arrays
enthalten.
</p>
</div>
</div>
@@ -24,7 +25,19 @@
<script>
'use strict';
const sumNestedArray = (arr) => {};
const sumNestedArray = (arr) => {
let sum = 0;
arr.forEach((value) => {
if (Array.isArray(value)) {
sum += sumNestedArray(value); // 1 + 2 + 3 +
} else if (typeof value === 'number') {
sum += value; // 4 + 5 + 6
}
});
return sum;
};
// Test cases
console.log(sumNestedArray([1, [2, [3, 4], 5], 6])); // => 21
@@ -0,0 +1,55 @@
// Variante 1
const printWords = (text = '', idx = 0) => {
const words = text.split(' '); //=> ['KI-Entwickler', 'warnen', 'UN-Gremium']
// Basisfall
if (idx >= words.length) return; // Abbruch
console.log(words[idx]); // KI-Entwickler
setTimeout(() => {
printWords(text, ++idx);
}, 1000);
};
// printWords('KI-Entwickler warnen UN-Gremium');
const printWordsFor = (sentence) => {
const wordArr = sentence.split(' ');
for (let i = 0; i < wordArr.length; i++) {
setTimeout(() => {
console.log(wordArr[i]);
}, 1000 * i);
}
};
printWordsFor('Bundestag berät über Organspende');
// setTimeout(() => {
// console.log('The');
// setTimeout(() => {
// console.log('pyramid');
// setTimeout(() => {
// console.log('of');
// setTimeout(() => {
// console.log('doom');
// setTimeout(() => {
// console.log('keeps');
// setTimeout(() => {
// console.log('growing.');
// }, 1000);
// }, 1000);
// }, 1000);
// }, 1000);
// }, 1000);
// }, 1000);
// Übung 11: Die Pyramide abbrechen
// Schreibe das Codebeispiel 96 so um, dass keine Pyramide entsteht. Benutze dazu, wie im Text angedeutet, eine Funktion, die das n-te Wort ausgibt und sich per setTimeout(…) mit n + 1 als Parameter aufruft, solange noch Wörter im Satz vorhanden sind.
@@ -0,0 +1,4 @@
name, category, price
Klingon Letter Opener, Office Warfare, 19.99
Backpack of Holding, Travel, 29.99
Tardis Alarmclock, Merchandise, 15.99
1 name category price
2 Klingon Letter Opener Office Warfare 19.99
3 Backpack of Holding Travel 29.99
4 Tardis Alarmclock Merchandise 15.99
@@ -0,0 +1,59 @@
// fs = FileSystem Modul aus der Node Standardbib (core)
import fs from 'node:fs'; // module - ESM Schreibweise
import util from 'node:util';
import color from 'chalk';
// Funktion mit einem asynchronen Prozess
const getFileBy = (path = '', encoding = 'utf-8') => {
return new Promise((resolve, reject) => {
// async process
fs.readFile(path, encoding, (error, data) => {
if (error) {
// console.error(error);
reject(error);
}
// console.log(data);
resolve(data);
});
});
};
const getPromisesFileBy = (path = '', encoding = 'utf-8') => {
// Unterobjekt "promises" von fs besitzt Ummantelung von Promise-Objekt und die Verarbeitung der Callback-Funktion
// async process
return fs.promises.readFile(path, encoding);
};
// util.promisify fängt das letzte Argument als Callback-Funktion ab und ummantel die Funktion mit einem Promise-Objekt.
// Das ummantelte Promise-Objekt wird zusätzlich von einer weiteren Funktion mit den Argumenten ummantelt.
// https://javascript.info/promisify
// https://medium.com/trabe/understanding-nodes-promisify-and-callbackify-d2b04efde0e0
const getPromisifyFileBy = util.promisify(fs.readFile);
// Funktionsaufruf einer Funktion mit asynchronem Prozess
getFileBy('./data/products.csv')
.then((data) => {
console.log(color.yellow(data));
})
.catch((err) => {
console.error(color.red(err));
});
getPromisesFileBy('./data/products.csv')
.then((data) => {
console.log(color.yellow(data));
})
.catch((err) => {
console.error(color.red(err));
});
getPromisifyFileBy('./data/products.csv', 'utf-8')
.then((data) => {
console.log(color.yellow(data));
})
.catch((err) => {
console.error(color.red(err));
});
@@ -0,0 +1,27 @@
{
"name": "05_promise-readfile",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "05_promise-readfile",
"version": "1.0.0",
"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"
}
}
}
}
@@ -0,0 +1,14 @@
{
"name": "07_fs-promises",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"type": "module",
"dependencies": {
"chalk": "^6.0.0"
}
}